class Solution { public String removeDuplicates(String S) { String result =""; int len = S.length(); Stack <Character> st = new Stack<Character>(); st.push(S.charAt(len-1)); if(S == null || S.length() ==0 ) { return ""; } else if(S.length()==1){ return S; } else { for(int i = S.length()-2; i>=0;i--){ if(st.empty()){ st.push(S.charAt(i)); } else{ if(S.charAt(i)== st.peek()){ st.pop(); } else{ st.push(S.charAt(i)); } } } if(st.empty()){ return result; } else{ do{ result = result + st.pop(); } while(!st.empty()); return result; } } } }
来源:https://www.cnblogs.com/codingyangmao/p/11277744.html