#1190
Reverse Substrings Between Each Pair of Parentheses
pupil · 535 · lc medium +28 · verified · 71.9% accepted · 2,940 likes · top 81%
Description
You receive a string s consisting of lowercase letters and parentheses. Process every matching pair of parentheses from the innermost outward, reversing the substring enclosed by each pair.
Your final output must not include any parenthesis characters.
Example 1:
Input: s = "(abcd)"
Output: "dcba"
Example 2:
Input: s = "(u(love)i)"
Output: "iloveu"
Explanation: The substring "love" is reversed first, then the whole string is reversed.
Example 3:
Input: s = "(ed(et(oc))el)"
Output: "leetcode"
Explanation: First, we reverse the substring "oc", then "etco", and finally, the whole string.
Code
1
2
3