Skip to content

Commit 1545d38

Browse files
authored
Update Solution.java
1 parent e265658 commit 1545d38

File tree

1 file changed

+13
-22
lines changed
  • solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses

1 file changed

+13
-22
lines changed
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
class Solution {
22
public String reverseParentheses(String s) {
3-
int n = s.length();
4-
int[] d = new int[n];
5-
Deque<Integer> stk = new ArrayDeque<>();
6-
for (int i = 0; i < n; ++i) {
7-
if (s.charAt(i) == '(') {
8-
stk.push(i);
9-
} else if (s.charAt(i) == ')') {
10-
int j = stk.pop();
11-
d[i] = j;
12-
d[j] = i;
13-
}
14-
}
15-
StringBuilder ans = new StringBuilder();
16-
int i = 0, x = 1;
17-
while (i < n) {
18-
if (s.charAt(i) == '(' || s.charAt(i) == ')') {
19-
i = d[i];
20-
x = -x;
3+
StringBuilder stk = new StringBuilder();
4+
for (char c : s.toCharArray()) {
5+
if (c == ')') {
6+
StringBuilder t = new StringBuilder();
7+
while (stk.charAt(stk.length() - 1) != '(') {
8+
t.append(stk.charAt(stk.length() - 1));
9+
stk.deleteCharAt(stk.length() - 1);
10+
}
11+
stk.deleteCharAt(stk.length() - 1);
12+
stk.append(t);
2113
} else {
22-
ans.append(s.charAt(i));
14+
stk.append(c);
2315
}
24-
i += x;
2516
}
26-
return ans.toString();
17+
return stk.toString();
2718
}
28-
}
19+
}

0 commit comments

Comments
 (0)