(, while a closing bracket is **two ) characters in a row**. A string is well-formed if every ( can be matched with its own )) pair somewhere to its right, and every ) belongs to exactly one such pair.
For instance, "())", "(())))" and "())(())))" are well-formed, while ")()", "()))" and "(()))" are not.
Given a string text of ( and ) characters, you may insert ( and ) anywhere. Return the fewest insertions that make it well-formed.text="(()" → 3
The inner ( is one ) short and the outer one is two short: "(())))", three insertions.
text="())))" → 1
The first )) closes the (, and the second )) needs its own (: "())(())", one insertion.
text=")((" → 6The lone ) needs a ( before it and a ) after it, and the two trailing ( need four more ): 6 in total.
text consists only of ( and )text="(()"
3
Run checks the first 3 tests in your browser. Submit checks all 118 tests on the server, hidden ones included.