File tree Expand file tree Collapse file tree 2 files changed +19
-13
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +19
-13
lines changed Original file line number Diff line number Diff line change 1
1
package com.fishercoder.solutions;
2
2
3
- import java.util.Stack;
3
+ import java.util.Deque;
4
+ import java.util.LinkedList;
4
5
5
6
public class _921 {
6
7
public static class Solution1 {
7
- public int minAddToMakeValid(String S ) {
8
- Stack <Character> stack = new Stack <>();
9
- for (char c : S .toCharArray()) {
8
+ public int minAddToMakeValid(String s ) {
9
+ Deque <Character> stack = new LinkedList <>();
10
+ for (char c : s .toCharArray()) {
10
11
if (c == ')') {
11
- if (!stack.isEmpty() && stack.peek () == '(') {
12
- stack.pop ();
12
+ if (!stack.isEmpty() && stack.peekLast () == '(') {
13
+ stack.pollLast ();
13
14
} else {
14
- stack.push (c);
15
+ stack.addLast (c);
15
16
}
16
17
} else {
17
- stack.push (c);
18
+ stack.addLast (c);
18
19
}
19
20
}
20
21
return stack.size();
Original file line number Diff line number Diff line change 1
1
package com.fishercoder;
2
2
3
3
import com.fishercoder.solutions._921;
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 _921Test {
10
10
private static _921.Solution1 solution1;
11
11
12
- @BeforeClass
13
- public static void setup() {
12
+ @BeforeEach
13
+ public void setup() {
14
14
solution1 = new _921.Solution1();
15
15
}
16
16
@@ -34,4 +34,9 @@ public void test4() {
34
34
assertEquals(4, solution1.minAddToMakeValid("()))(("));
35
35
}
36
36
37
+ @Test
38
+ public void test5() {
39
+ assertEquals(1, solution1.minAddToMakeValid(")()"));
40
+ }
41
+
37
42
}
You can’t perform that action at this time.
0 commit comments