Skip to content

Commit 332c32f

Browse files
authored
Create 1190. Reverse Substrings Between Each Pair of Parentheses (#526)
2 parents 4d17aa0 + b5b92ae commit 332c32f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
void reverseString(string &s, int i, int j)
4+
{
5+
while(i < j)
6+
{
7+
if(s[i] == '(') i++;
8+
if(s[j] == ')') j--;
9+
swap(s[i++], s[j--]);
10+
}
11+
}
12+
string reverseParentheses(string s)
13+
{
14+
stack<int>st;
15+
int n = s.size();
16+
for(int i = 0; i < n; i++)
17+
{
18+
if(s[i] == '(') st.push(i);
19+
if(s[i] == ')')
20+
{
21+
reverseString(s, st.top(), i);
22+
st.pop();
23+
}
24+
}
25+
26+
string ans;
27+
for(auto ch:s)
28+
if(isalpha(ch)) ans += ch;
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
 (0)