Check If Word Is Valid After Substitutions LeetCode solution in C++

1
class Solution {
2
public:
3
bool isValid(string s) {
4
stack<char> st;
5
for (int i = 0; i < s.length(); i++) {
6
if (s[i] == 'a')
7
st.push(s[i]);
8
else if (s[i] == 'b') {
9
if (st.empty() || st.top() != 'a')
10
return false;
11
else
12
st.push(s[i]);
13
} else if (s[i] == 'c') {
14
if (st.empty() || st.top() != 'b')
15
return false;
16
else {
17
for (int i = 0; i < 2; i++) st.pop();
18
}
19
}
20
}
21
return st.size() == 0;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0