-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheck_1003.java
35 lines (31 loc) · 970 Bytes
/
Check_1003.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Stack;
public class Check_1003 {
// Most voted solution using a stack
// Keep a stack, whenever meet 'c',
// pop a and b at the end of stack.
// Otherwise return false.
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (char c: s.toCharArray()) {
if (c == 'c') {
if (stack.isEmpty() || stack.pop() != 'b') return false;
if (stack.isEmpty() || stack.pop() != 'a') return false;
} else {
stack.push(c);
}
}
return stack.isEmpty();
}
// My solution: Brute Force
/*
public boolean isValid(String S) {
if (S.length() % 3 != 0) return false;
while (!S.isEmpty() && S.contains("abc")) {
// int idx = S.indexOf("abc");
// S = S.substring(0, idx) + S.substring(idx + 3);
S = S.replace("abc", "");
}
return S.isEmpty();
}
*/
}