题目意思是给一个字符串aabbbassa,消除掉超过两次连续的字母,直到不能消除。
package 快手笔试;
import java.util.Scanner;
import java.util.Stack;
public class Solution1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] ch = str.toCharArray();
solve(ch);
}
private static void solve(char[] ch) {
Stack<char[]> st = new Stack<>();
for(int i = 0; i < ch.length; i++){
char[] temp = new char[2];
temp[0] = ch[i];
if(st.isEmpty() || st.peek()[0] != ch[i]){
temp[1] = '1';
st.push(temp);
}else{
if(st.peek()[1] == '1'){
temp[1] = '2';
st.push(temp);
continue;
}
int index = i;
while (ch[index] == ch[i]){
index++;
if(index == ch.length)
break;
}
i = index-1;
st.pop();
st.pop();
}
}
Stack<Character> st2 = new Stack<>();
while (!st.isEmpty())
st2.push(st.pop()[0]);
while (!st2.isEmpty())
System.out.print(st2.pop());
}
}