-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL678-ValidParenthesisString.java
More file actions
32 lines (32 loc) · 1001 Bytes
/
Copy pathL678-ValidParenthesisString.java
File metadata and controls
32 lines (32 loc) · 1001 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
28
29
30
31
32
// https://leetcode.com/problems/valid-parenthesis-string/
class Solution {
public boolean checkValidString(String s) {
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> starStack = new Stack<Integer>();
int length = s.length();
for (int i = 0; i < length; i++) {
if (s.charAt(i) == '(') {
stack.push(i);
} else if (s.charAt(i) == '*') {
starStack.push(i);
} else {
if (!stack.isEmpty()) {
stack.pop();
} else if (!starStack.isEmpty()) {
starStack.pop();
} else {
return false;
}
}
}
while (!stack.isEmpty() && !starStack.isEmpty()) {
if (stack.pop() > starStack.pop()) {
return false;
}
}
if (!stack.isEmpty()) {
return false;
}
return true;
}
}