← Pointer Manipulation

Micro-Drill #150 — Valid parenthesis string greedy

Pointer Manipulation Target: 10s

Track low/high range of possible open counts. Wildcard expands the range.

lo = hi = 0
for c in s:
    lo += 1 if c == '(' else -1
    hi += 1 if c != ')' else -1
    if hi < 0: return False
    lo = max(lo, 0)
return lo == 0

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #149 Micro #151 →