1190. 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { string helper(string&s, int& p) { int n = s.length(); string res = ""; while(p < n and s[p] != ')') { if(s[p] == '(') { ++p; string rev = helper(s, p); reverse(begin(rev), end(rev)); res += rev; } else { res.push_back(s[p++]); } } p++; return res; } public: string reverseParentheses(string s) { int p = 0; return helper(s, p); } };
|