package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
String s = "abbaca";
String re = removeDuplicates(s);
int i=0;
}
public static String removeDuplicates(String s) {
if (s == null || s.length() == 0) {
return "";
}
StringBuffer res = new StringBuffer();//用于模拟栈
int top = -1;//等于-1时,res没有元素
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (top >= 0 && res.charAt(top) == c) {//栈中有元素且和栈顶元素相同时,弹出栈顶元素
res.deleteCharAt(top);
top--;
} else {
res.append(c);
top++;
}
}
return res.toString();
}
}