FunctionRacerProblemsRace

Drop the Unmatched Brackets

EasyStacks and queuesPython, JavaScript, TypeScript, Java, C++

s holds lowercase letters and the brackets ( and ). Read it from left to right: a ) is matched with the nearest unmatched ( before it. A ) with no unmatched ( before it is unmatched, and so is every ( still unmatched at the end.

Return s with every unmatched bracket removed. Everything else keeps its order.

Example 1

s "a)b(c)d("
returns "ab(c)d"

The ) at index 1 has nothing to match, and the last ( is never closed.

Example 2

s "))(("
returns ""

Both ) come before every (, so nothing matches.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems