1190. Reverse Substrings Between Each Pair of Parentheses LeetCode Solution
In this guide, you will get 1190. Reverse Substrings Between Each Pair of Parentheses LeetCode Solution with the best time and space complexity. The solution to Reverse Substrings Between Each Pair of Parentheses problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Reverse Substrings Between Each Pair of Parentheses solution in C++
- Reverse Substrings Between Each Pair of Parentheses solution in Java
- Reverse Substrings Between Each Pair of Parentheses solution in Python
- Additional Resources

Problem Statement of Reverse Substrings Between Each Pair of Parentheses
You are given a string s that consists of lower case English letters and brackets.
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
Your result should not contain any brackets.
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.
Constraints:
1 <= s.length <= 2000
s only contains lower case English characters and parentheses.
It is guaranteed that all parentheses are balanced.
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n)
1190. Reverse Substrings Between Each Pair of Parentheses LeetCode Solution in C++
class Solution {
public:
string reverseParentheses(string s) {
stack<int> stack;
string ans;
for (const char c : s)
if (c == '(') {
stack.push(ans.length());
} else if (c == ')') {
// Reverse the corresponding substring between ().
const int j = stack.top();
stack.pop();
reverse(ans.begin() + j, ans.end());
} else {
ans += c;
}
return ans;
}
};
/* code provided by PROGIEZ */
1190. Reverse Substrings Between Each Pair of Parentheses LeetCode Solution in Java
class Solution {
public String reverseParentheses(String s) {
Deque<Integer> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder();
for (final char c : s.toCharArray())
if (c == '(') {
stack.push(sb.length());
} else if (c == ')') {
// Reverse the corresponding substring between ().
StringBuilder reversed = new StringBuilder();
for (int sz = sb.length() - stack.pop(); sz > 0; --sz) {
reversed.append(sb.charAt(sb.length() - 1));
sb.deleteCharAt(sb.length() - 1);
}
sb.append(reversed);
} else {
sb.append(c);
}
return sb.toString();
}
}
// code provided by PROGIEZ
1190. Reverse Substrings Between Each Pair of Parentheses LeetCode Solution in Python
class Solution:
def reverseParentheses(self, s: str) -> str:
stack = []
ans = []
for c in s:
if c == '(':
stack.append(len(ans))
elif c == ')':
# Reverse the corresponding substring between ().
j = stack.pop()
ans[j:] = ans[j:][::-1]
else:
ans.append(c)
return ''.join(ans)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.