Problem · String

Count Balanced Bracket Insertions

Learn this problem
MediumAtlassian logoAtlassianFULLTIMEOA

Problem 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) → int

Examples

Example 1

s = "()())"return = 5

The sequence has one extra closing bracket. Inserting an opening bracket at any of five valid positions produces a balanced sequence.

Constraints

  • 1 <= s.length <= 200000
  • s.length is odd.
  • s contains only ( and ).

More Atlassian problems

drafts saved locally
public int countBalancedInsertions(String s) {
  // Write your code here.
}
s"()())"
expected5
checking account