forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
40 lines (40 loc) · 1.05 KB
/
Solution.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
36
37
38
39
40
class Solution {
public boolean checkValidString(String s) {
int n = s.length();
char[] a = s.toCharArray();
int left = 0, asterisk = 0;
for (int i = 0; i < n; i++) {
if (a[i] == '(') {
left++;
} else if (a[i] == ')') {
if (left > 0) {
left--;
} else if (asterisk > 0) {
asterisk--;
} else {
return false;
}
} else {
asterisk++;
}
}
int right = 0;
asterisk = 0;
for (int i = n - 1; i >= 0; i--) {
if (a[i] == ')') {
right++;
} else if (a[i] == '(') {
if (right > 0) {
right--;
} else if (asterisk > 0) {
asterisk--;
} else {
return false;
}
} else {
asterisk++;
}
}
return true;
}
}