forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1003.java
More file actions
27 lines (23 loc) · 722 Bytes
/
_1003.java
File metadata and controls
27 lines (23 loc) · 722 Bytes
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
package com.fishercoder.solutions;
import java.util.ArrayDeque;
import java.util.Deque;
public class _1003 {
public static class Solution1 {
public boolean isValid(String S) {
Deque<Character> stack = new ArrayDeque<>();
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();
}
}
}