Trains the technique from
LeetCode 678Valid Parenthesis StringThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A packaging tool records a trace while it walks a project. It writes ( when it enters a span and ) when it leaves one, and a leaving mark always settles the span that was entered most recently and is still open. A trace is sound when every leaving mark settles some earlier entering mark and no span is left open once the trace runs out.
One run wrote to a failing disk, so a few bytes came back smudged. A smudged byte is recorded as *, and each one may have held an entering mark, a leaving mark, or no mark at all. Two smudged bytes need not be read the same way.
You are handed the recovered trace s. Return true when at least one reading of the smudged bytes leaves a sound trace, and false when no reading does.
Example 1
Read both smudged bytes as carrying no mark and the trace becomes two entering marks followed by two leaving marks, which is sound.
Example 2
The trace finishes with two entering marks and nothing after them, so two spans stay open no matter how the leading smudge is read.
Example 3
Reading both bytes as carrying no mark leaves an empty trace, which is sound; reading the first as entering and the second as leaving works too.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def trace_is_recoverable(s: str) -> bool:public boolean traceIsRecoverable(String s)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.