Problem · String
Count Balanced Bracket Insertions
Learn this problemProblem statement
Given an odd-length string s containing only parentheses, insert exactly one '(' or ')' at any of the s.length + 1 positions.
Count the insertion choices that make the resulting sequence balanced. Choices are counted by insertion position, even when two positions produce the same final text.
Function
countBalancedInsertions(s: String) → intExamples
Example 1
s = "()())"return = 5The sequence has one extra closing bracket. Inserting an opening bracket at any of five valid positions produces a balanced sequence.
Constraints
1 <= s.length <= 200000s.lengthis odd.scontains only(and).