-
Notifications
You must be signed in to change notification settings - Fork 0
/
1003.cpp
76 lines (74 loc) · 1.55 KB
/
1003.cpp
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
class Solution {
public:
bool isValid(string S) {
// return func1(S);
return func2(S);
}
// ** ugly if-else branch hell
bool func1(string S) {
int N = S.length();
int i = 0;
int j = N - 1;
while (j > i) {
if (S[i] == 'a') {
if (i + 1 <= j && S[i + 1] == 'b') {
if (i + 2 <= j && S[i + 2] == 'c') {
i += 3;
} else {
if (S[j] == 'c') {
i += 2;
j -= 1;
} else {
return false;
}
}
} else {
if (j - 1 >= 0 && S[j - 1] == 'b' && S[j] == 'c') {
i += 1;
j -= 2;
} else {
return false;
}
}
} else if (S[i] == 'b') {
if (i + 1 < N && S[i + 1] == 'c' && S[j] == 'a') {
i += 2;
j -= 1;
} else {
return false;
}
} else if (S[i] == 'c') {
if (j - 1 >= 0 && S[j - 1] == 'a' && S[j] == 'b') {
i += 1;
j -= 2;
} else {
return false;
}
}
}
return true;
}
// ** stack
bool func2(string S) {
stack<char> stk;
int i = 0;
for (auto s : S) {
if (s == 'c') {
if (stk.size() < 2) {
return false;
} else {
char b = stk.top();
stk.pop();
char a = stk.top();
stk.pop();
if (b != 'b' || a != 'a') {
return false;
}
}
} else {
stk.push(s);
}
}
return stk.empty();
}
};