-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path678.java
More file actions
33 lines (33 loc) · 1.13 KB
/
678.java
File metadata and controls
33 lines (33 loc) · 1.13 KB
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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public boolean checkValidString(String s) {
int lo = 0, hi = 0;
for (char c: s.toCharArray()) {
lo += c == '(' ? 1 : -1;
hi += c != ')' ? 1 : -1;
if (hi < 0) break;
lo = Math.max(lo, 0);
}
return lo == 0;
}
}
__________________________________________________________________________________________________
sample 35120 kb submission
class Solution {
public boolean checkValidString(String s) {
int bal = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == '*') bal++;
else if (bal-- <= 0) return false;
}
if (bal == 0) return true;
bal = 0;
for (int i = s.length()-1; i >= 0; i--) {
if (s.charAt(i) == ')' || s.charAt(i) == '*') bal++;
else if (bal-- == 0) return false;
}
return true;
}
}
__________________________________________________________________________________________________