Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
publicStringremoveDuplicateLetters(String s) {if (s ==null||s.isEmpty()) {return""; }char[] cs =s.toCharArray();Stack<Character> stack =newStack<>();Set<Character> visited =newHashSet<>();Map<Character,Integer> count =newHashMap<>();// get the counts for each charfor (char c : cs) {if (count.containsKey(c)) {count.put(c,count.get(c) +1); } else {count.put(c,1); } }for (int i =0; i <cs.length; i++) {char c = cs[i];count.put(c,count.get(c) -1);// if stack contains the char already, we skipif (visited.contains(c)) {continue; }// remove chars that are dup but has larger lexicalgraphic order in stack// count.get(c) > 0 means there are dup still to come. // eg stack = bc, remaining abc, a can pop b,c because there are b, c after awhile (!stack.isEmpty() && c <stack.peek() &&count.get(stack.peek()) >0) {visited.remove(stack.pop()); }stack.push(c);visited.add(c); }StringBuilder sb =newStringBuilder();while (!stack.isEmpty()) {sb.insert(0,stack.pop()); }returnsb.toString();}