classSolution{ public String removeDuplicates(String S){ StringBuffer sb = new StringBuffer(); int len = S.length(); Stack<Character> stack = new Stack<>(); if(len==1){ return S; }else{ for (int i = 0; i < len; i++) { if(stack.empty()){ … } } for (Character character : stack) { sb.append(character); } return sb.toString(); } }