MediumGreedy · Stack · Dynamic Programming

Valid Parenthesis String

s contains only (, ) and *. Each * can be read as (, ) or nothing. Return true if some reading makes every parenthesis matched in order.

s = "((**)" → true (one * as ")", one empty)s = "(((*)" → false

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

The string

The string, with how each character is used

Time and space complexity

n = len(s). The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. Only ( ) and *; drawings use at most 8 characters for top-down DP and 20 for the others.