You get a string s of lowercase letters and parentheses. Every ( has its own ).
Return the string that is left when all the parentheses are gone.
Remove them from the inside out. Take an innermost pair and reverse the letters between its brackets. Then delete the two brackets. Repeat until no brackets are left.
Example 1:
Input: s = "my(tac)" Output: "mycat"
Example 2:
Input: s = "(ek(ca)nap)" Output: "pancake" Explanation: The inner pair turns "ca" into "ac". That leaves "(ekacnap)". Reversing it gives "pancake".
Example 3:
Input: s = "(ab)()(cd)" Output: "badc" Explanation: "ab" becomes "ba". "cd" becomes "dc". The empty pair adds nothing.
Constraints:
shas1to2,000characters.- Each character is a lowercase English letter,
(or). - Every bracket is matched.
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
