CT34196

The Bracket Repair of the Timeworn charms

MediumL4 · Loop LordAcceptance: 0.0%XP: 35

Petr runs the mill at the northern keep of Frostgate. Today the books show a list of charms, and the numbers must be worked out before sundown. Task: Print the minimum number of parentheses (either kind) that must be inserted to make the sequence balanced. Input format: One line with a string of parentheses. Output format: Print the minimum insertion count. Constraints: 1 <= |s| <= 31166; characters are '(' and ')'.

Example 1:

Input: )((())))))(())()(((((((())((())))(()()))()())))(()()(((()))()((((())((())()(()())()()))()(())()))()(()(((())((((((())()()))((((())()()(())())))(()))))))(()()()()())((((()))((())))())(()()((())())))((()((()(())()(()())()()(()(())())))()))()())))((())))))((()(((())(()())())(()((((()(((()(()((((()))))()()()()()))(())))())()((((())))))()())(()()()(()))))))()()())())))()))()()))))()((()())(((()))()()())()((()(()((())())))()((())((()(()))))((((())()(())()()()))((((((()(()(()()(())(()(())()())())))((((())())()))((()()())(()(()))())()))((()()())(((())((()())))()(()(((((())()))((()()()))()())()((((()(()()(()()((()))(((())))))))((((()))(()(()()()(()()))))))())(())))(()()())(()())()(()((())(()()))))()()()))(()))(((((()))())))(((()(()()(()((((()()(((())()((())((((()(())(())(())()(()))()()((()())))))((…
Output: 166

Example 2:

Input: (()))))(())()(()))((())))((()))())))(()()())))()))())()((((((()(()()))(((())())(((()())(((())))((())())))(()((())()(())())()()((((()(())(((()(()())()(()()((())()()()())(((())))))(((()(((()((((()(((((()()(()()()((((()()))(()()((()()))((((())()(())))()()))(()))()()(()((()())(()()))()(()()))))))(((()()())(((())(((((()))))()))((()(((()())(())))((()))()))()(()())(()())())())(()()(()(((())(((((((((())(()()))())(())())()(()()(()())(()))()(((()))(())))(()()()((()))(((())(((((()))(())((()))()))(()))))()()()()(()(()())(())))())(()))(())((((())(()((()())))))))(((()(())(())()()(((((())))()()(((()))((()))((()(()(()(())())())))()(()()()((((()()())(()((())((()((()())(()((()((()))(()(()(((()((()))))())(())()(()))()((())))(((())(()())()()))))())()(()))))()()((((())))))((()())))())(()()()))))(()(()())()(()(…
Output: 200

Example 3:

Input: )())()))))))))))(((())()(())((())()(()()))))())))))(()(()((())))))))((()()))((()()))))()(())(((()(()()()((()))()))))(((()((())))((()()((()))(()()(())))(())()()()(())((())))))(()((((((((()()())((()()))())()())(()(()()())(())))(((()))(((()(()))))(()()(((())()(()()()((())())))))))(((())((()())()))(())))()))()((((((((((())()()()))((()((()((()))())()((()))()()(())()())(()))()))())))(((())())))())((((((()()(((((()()()())))))(()()))))(()(())()(()(())(()()))(()))(((((((()(((()(())((((((()((())))()())()(()()))()))))(())))()))(((((()()())()())(()))()))((((((((()))(()))((()((()(())((()(()))())(())()(((()())))(()))((())))(()((()(())()((()((())(((()((()(()(((()()))))()()())((()(((()(())(()))))((()(()))((()())(()())()((()()()((((()(())()((()((()))()))()))))))()()())(((()(())))(()))()()()()(()()())))))((…
Output: 106

Constraints:

1 <= |s| <= 31166; characters are '(' and ')'.

Loading...
Test Cases:No test cases
No test cases available.
The Bracket Repair of the Timeworn charms - MEDIUM Coding Problem | CodeTikki