-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
1003.java
84 lines (69 loc) · 2.23 KB
/
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public boolean isValid(String S) {
if(S.length()<3){
return false;
}
String base="abc";
if(S.length()==3 && !S.equals(base)){
return false;
}
return valid(S.toCharArray(),0,S.length()-1);
}
public boolean valid(char[] s, int i, int j){
if(j-i==2 && s[i]=='a' && s[i+1]=='b' && s[i+2]=='c'){
return true;
}else if(j-i<2){
return false;
}
if(s[i]=='a' && s[i+1]=='b' && s[i+2]=='c'){
if(valid(s,i+3,j)){
return true;
}
}
if(s[i]=='a' && s[i+1]=='b' && s[j]=='c'){
if(valid(s,i+2,j-1)){
return true;
}
}
if(s[i]=='a' && s[j-1]=='b' && s[j]=='c'){
if(valid(s,i+1,j-2)){
return true;
}
}
if(s[j-2]=='a' && s[j-1]=='b' && s[j]=='c'){
if(valid(s,i,j-3)){
return true;
}
}
if(s[i]=='a' && s[(j-i)/2]=='b' && s[j]=='c' ){
int temp= (j-i)/2;
if(valid(s,i+1,temp-1) && valid(s,temp+1,j-1)){
return true;
}
}
return false;
}
}
__________________________________________________________________________________________________
sample 38024 kb submission
class Solution {
public boolean isValid(String S) {
Deque<Character> stack = new ArrayDeque<>();
for (char c : S.toCharArray()) {
if (c == 'c') {
if (stack.peek() == null || stack.pop() != 'b') {
return false;
}
if (stack.peek() == null || stack.pop() != 'a') {
return false;
}
} else {
stack.push(c);
}
}
return stack.peek() == null;
}
}
__________________________________________________________________________________________________