Skip to content
Decoder

Valid Parenthesis String

Medium

Problem

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 (.

Example 1

Input
s = "()"
Output
true

Example 2

Input
s = "(*)"
Output
true
Why
* is empty

Example 3

Input
s = "(*))"
Output
true
Why
* is a "("

Example 4

Input
s = "(("
Output
false

Example 5

Input
s = "*)("
Output
false
Why
the last "(" is never closed

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.