Skip to content

Valid Parenthesis String

MediumGreedy
Decode it first. Clues, constraints, pattern and plan before any code.

s contains only (, ) and *. Each * is a wildcard: it can act as (, as ), or as nothing at all. Return true if some choice of wildcards makes s a balanced bracket string: every ( is closed by a later ), and every ) closes an earlier (.

s = "()"     →  true
s = "(*)"    →  true    (* is empty)
s = "(*))"   →  true    (* is a "(")
s = "(("     →  false
s = "*)("    →  false   (the last "(" is never closed)

Constraints

  • 1 <= s.length <= 100
  • s[i] is (, ) or *.