力扣1003
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for (char c : s) {
st.push(c);
// 检查栈顶是否形成 "abc"
while (st.size() >= 3) {
char third = st.top(); st.pop();
char second = st.top(); st.pop();
char first = st.top(); st.pop();
// 如果不是"abc",就把字符放回栈中
if (!(first == 'a' && second == 'b' && third == 'c')) {
st.push(first);
st.push(second);
st.push(third);
break;
}
// 如果是"abc",就不把它们放回栈中(相当于移除这三个字符)
}
}
// 如果栈为空,说明所有字符都能组成"abc"的组合
return st.empty();
}
};