Balanced Bracket Check With Wildcard Characters

Implement checkValidString

Given a string s containing '(', ')', and '?' — where each '?' can stand in for '(', for ')', or for nothing at all — determine whether some assignment of the wildcards makes the string a balanced bracket sequenceBalancedEvery '(' has a matching ')' later in the string, and every ')' has a matching '(' earlier — with '?' free to become whichever character (or nothing) makes that possible.. Trying every combination of wildcard assignments is correct but explodes exponentially. The key insight that avoids it: instead of picking one interpretation for each '?' up front, track the entire range of open-bracket counts that remain simultaneously possible as the string is scanned — the fewest opens achievable so far, and the most. A '?' simply widens that range by one on each side rather than forcing a choice. As long as 0 stays reachable within the range by the end, some valid assignment exists — found in a single linear pass instead of a search tree.

Example 1:

Input: s = "(())?"

Output: true

Example 2:

Input: s = "?)?)"

Output: true

Example 3:

Input: s = "(?))("

Output: false

+ 7 hidden test cases run on Submit.

Constraints:

  • 1 ≤ s.length ≤ 20
  • s consists only of the characters '(', ')', and '?'
  • '?' may stand in for '(', for ')', or for nothing at all (an empty character)

s =

(())?