@@ -0,0 +1,1700 @@
+package com.interview.stackqueue;%0A%0Aimport java.util.*;%0A%0A/**%0A * Date 02/29/2016%0A * @author Tushar Roy%0A *%0A * Given a string remove duplicates from the string maintaining order%0A * and getting lexicographically smallest string.%0A *%0A * Reference:%0A * https://leetcode.com/problems/remove-duplicate-letters/%0A */%0Apublic class RemoveDuplicateMaintainingOrder %7B%0A    public String removeDuplicateLetters(String s) %7B%0A        Deque%3CCharacter%3E stack = new LinkedList%3C%3E();%0A        Map%3CCharacter, Integer%3E count = new HashMap%3C%3E();%0A        for (int i = 0; i %3C s.length(); i++) %7B%0A            count.compute(s.charAt(i), (key, val) -%3E %7B%0A                if (val == null) %7B%0A                    return 1;%0A                %7D else %7B%0A                    return val + 1;%0A                %7D%0A            %7D);%0A        %7D%0A%0A        Set%3CCharacter%3E visited = new HashSet%3C%3E();%0A        for (int i = 0; i %3C s.length(); i++) %7B%0A            char ch = s.charAt(i);%0A            count.put(ch, count.get(ch) - 1);%0A            if (visited.contains(ch)) %7B%0A                continue;%0A            %7D%0A            while (!stack.isEmpty() && stack.peekFirst() %3E ch && count.get(stack.peekFirst()) %3E 0) %7B%0A                visited.remove(stack.peekFirst());%0A                stack.pollFirst();%0A            %7D%0A%0A            stack.offerFirst(ch);%0A            visited.add(ch);%0A        %7D%0A%0A        StringBuffer buff = new StringBuffer();%0A        while (!stack.isEmpty()) %7B%0A            buff.append(stack.pollLast());%0A        %7D%0A        return buff.toString();%0A    %7D%0A%0A    public static void main(String args%5B%5D) %7B%0A        RemoveDuplicateMaintainingOrder rm = new RemoveDuplicateMaintainingOrder();%0A        System.out.println(rm.removeDuplicateLetters(%22cbacdcbc%22));%0A    %7D%0A%7D%0A

remove duplicates by maintaing order;