OracleOA #2
Balancing Parentheses
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & HeapsStrings
Only insertions of parentheses are allowed. Find the minimum number of insertions needed to turn the original string into a valid parenthesis string.
Input
One line containing a string of only ( and ), with full original bounds 1≤length≤100000.
Output
Output a single integer, the answer.
Example 1
Input:
()))
Output:
2
Example 2
Input:
))((
Output:
4
Example 3
Input:
()()
Output:
0
Example notes
The three examples need 2, 4, and 0 insertions respectively. The second can become (())(()); the source's (()))(()) is wrong in both length and validity and has been corrected.
Editorial
Loading solution…