Skip to content

Commit b561f4b

Browse files
update 921
1 parent 5ea5695 commit b561f4b

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_921.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.Stack;
3+
import java.util.Deque;
4+
import java.util.LinkedList;
45

56
public class _921 {
67
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()) {
1011
if (c == ')') {
11-
if (!stack.isEmpty() && stack.peek() == '(') {
12-
stack.pop();
12+
if (!stack.isEmpty() && stack.peekLast() == '(') {
13+
stack.pollLast();
1314
} else {
14-
stack.push(c);
15+
stack.addLast(c);
1516
}
1617
} else {
17-
stack.push(c);
18+
stack.addLast(c);
1819
}
1920
}
2021
return stack.size();

Diff for: src/test/java/com/fishercoder/_921Test.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.fishercoder;
22

33
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;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _921Test {
1010
private static _921.Solution1 solution1;
1111

12-
@BeforeClass
13-
public static void setup() {
12+
@BeforeEach
13+
public void setup() {
1414
solution1 = new _921.Solution1();
1515
}
1616

@@ -34,4 +34,9 @@ public void test4() {
3434
assertEquals(4, solution1.minAddToMakeValid("()))(("));
3535
}
3636

37+
@Test
38+
public void test5() {
39+
assertEquals(1, solution1.minAddToMakeValid(")()"));
40+
}
41+
3742
}

0 commit comments

Comments
 (0)