Given a string $P$ consisting of only parentheses and asterisk characters (i.e. "(", ")" and "*"), you are asked to replace all the asterisk characters in order to get a balanced parenthesis string with the shortest possible length, where you can replace each "*" by one "(", or one ")", or an empty string "".
A parenthesis string $S$ is a string consisting of only parentheses (i.e. "(" and ")"), and is considered balanced if and only if:
● $S$ is an empty string, or
● there exist two balanced parenthesis strings $A$ and $B$ such that $S = A B$, or
● there exists a balanced parenthesis string $C$ such that $S = (C)$.
For instance, "", "()", "(())", "()()", "()(())" are balanced parenthesis strings.
Due to some notorious technical inability, if there are several solutions with the shortest possible length, then you have to report the smallest possible one in lexicographical order.
For every two different strings $A$ and $B$ of the same length $n$, we say $A$ is smaller than $B$ in lexicographical order if and only if there exists some integer $k$ such that:
● $1 \leq k \leq n$, and
● the first $(k - 1)$ characters of $A$ and that of $B$ are exactly the same, and
● the $k$-th character of $A$ is smaller than that of $B$.
For instance, "()(())" is smaller than "()()()", and in this case, $k = 4$.