- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1003.java
27 lines (23 loc) · 737 Bytes
/
_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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.ArrayDeque;
importjava.util.Deque;
publicclass_1003 {
publicstaticclassSolution1 {
publicbooleanisValid(StringS) {
Deque<Character> stack = newArrayDeque<>();
for (charc : S.toCharArray()) {
if (c == 'c') {
if (stack.isEmpty() || stack.pop() != 'b') {
returnfalse;
}
if (stack.isEmpty() || stack.pop() != 'a') {
returnfalse;
}
} else {
stack.push(c);
}
}
returnstack.isEmpty();
}
}
}