#678

Valid Parenthesis String

Medium Range of open counts

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

A string has '(', ')' and '*', where '*' can be '(', ')' or empty. Decide whether it can be a valid parentheses string.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: with '*' as '(', ')' or nothing, can the string be valid?

Why this works: track the possible range of open brackets [lo, hi]; '*' widens it.

Example: "(*))"

'(' → [1,1], '*' → [0,2], ')' → [0,1], ')' → [0,0]lo reaches 0 → valid

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Treating every '*' as one fixed choice — you need to keep all options open.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Greedy
🎬 Episodes 69–73: video coming soon