← DiffPush

Valid Parenthesis String

Baseline Greedy Approach · Easy O(n) · O(1)

The Wildcard Ledger Reconciliation

A reconciliation script parses ledger lines containing opens, closes, and wildcard tokens that may act as either — or vanish entirely. A line is acceptable if some assignment of the wildcards makes the brackets balance in order. The auditor tracks the lowest and highest plausible open-count while scanning, and accepts the line only if zero opens remains reachable at the end.

Input: A string s containing only '(', ')' and '*'.

Output: Return true if s can be a valid parenthesis sequence under some wildcard assignment.

Constraints

Examples

Example 1

Input: {"s":"(*))"}
Output: true
The wildcard plays the opener, matching the two closers that follow.

Example 2

Input: {"s":"(*)"}
Output: true
Here the wildcard acts as the closer.

Solve this in your browser →

Also on LeetCode ↗