File tree 2 files changed +37
-5
lines changed
main/java/com/fishercoder/solutions/secondthousand
test/java/com/fishercoder/secondthousand
2 files changed +37
-5
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions .secondthousand ;
2
2
3
+ import java .util .Deque ;
3
4
import java .util .LinkedList ;
4
5
import java .util .Queue ;
5
6
import java .util .Stack ;
@@ -31,4 +32,32 @@ public String reverseParentheses(String s) {
31
32
return sb .reverse ().toString ();
32
33
}
33
34
}
35
+
36
+ public static class Solution2 {
37
+
38
+ public static String reverseParentheses (String s ) {
39
+ Deque <String > stack = new LinkedList <>();
40
+ for (char c : s .toCharArray ()) {
41
+ if (c == '(' || Character .isAlphabetic (c )) {
42
+ stack .addLast (c + "" );
43
+ } else {
44
+ StringBuilder innerSb = new StringBuilder ();
45
+ while (!stack .isEmpty ()) {
46
+ if (stack .peekLast ().equals ("(" )) {
47
+ stack .pollLast ();
48
+ break ;
49
+ } else {
50
+ innerSb .append (stack .pollLast ());
51
+ }
52
+ }
53
+ stack .addLast (innerSb .reverse ().toString ());
54
+ }
55
+ }
56
+ StringBuilder sb = new StringBuilder ();
57
+ while (!stack .isEmpty ()) {
58
+ sb .append (stack .pollLast ());
59
+ }
60
+ return sb .reverse ().toString ();
61
+ }
62
+ }
34
63
}
Original file line number Diff line number Diff line change 1
1
package com .fishercoder .secondthousand ;
2
2
3
3
import com .fishercoder .solutions .secondthousand ._1190 ;
4
- import org .junit .BeforeClass ;
5
- import org .junit .Test ;
4
+ import org .junit .jupiter . api . BeforeEach ;
5
+ import org .junit .jupiter . api . Test ;
6
6
7
- import static org .junit .Assert .assertEquals ;
7
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
8
8
9
9
public class _1190Test {
10
10
private static _1190 .Solution1 solution1 ;
11
+ private static _1190 .Solution2 solution2 ;
11
12
12
- @ BeforeClass
13
- public static void setup () {
13
+ @ BeforeEach
14
+ public void setup () {
14
15
solution1 = new _1190 .Solution1 ();
16
+ solution2 = new _1190 .Solution2 ();
15
17
}
16
18
17
19
@ Test
18
20
public void test1 () {
19
21
assertEquals ("dcba" , solution1 .reverseParentheses ("(abcd)" ));
22
+ assertEquals ("dcba" , solution2 .reverseParentheses ("(abcd)" ));
20
23
}
21
24
22
25
@ Test
You can’t perform that action at this time.
0 commit comments