Drop the Unmatched Brackets
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
0 <= len(s) <= 60sholds lowercase letters,(and)only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.