题目如:abbbaac
消除结果是c
因为leetcode也没有刷题链接,所以自己写了几个测试用例通过了一下,以下是代码:
string xiaoxiaole(string s){
if(s.length()<=2) return s;
stack<char> save;
save.push(s[0]);
int count=1;
int i=1;
string res = "";
while(i<s.length()){
if(!save.empty() && save.top()==s[i]){
count++;
int id = i+1;
while(id<s.length() && s[id]==s[i]) {
count ++;
id++;
}
if(count>=3){//说明连上了,要消除
while(!save.empty() && save.top()==s[i]){
save.pop();
}
if(!save.empty())
count = 1;
else
count = 0;
i=id;
}
}else{
save.push(s[i]);