package leetcode;

/**
 * https://leetcode.com/problems/remove-duplicate-letters/
 */
public class Problem316 {
    public String removeDuplicateLetters(String s) {
        StringBuilder sb = new StringBuilder();
        return sb.toString();
    }

    public static void main(String[] args) {
        Problem316 prob = new Problem316();
//        System.out.println(prob.removeDuplicateLetters("bcabc")); // abc
//        System.out.println(prob.removeDuplicateLetters("bbcaac")); // bac
        System.out.println(prob.removeDuplicateLetters("cbacdcbc")); // acdb
    }
}

@@ -12,16 +12,91 @@
 tcode;%0A%0A
+import java.util.Set;%0Aimport java.util.TreeMap;%0Aimport java.util.TreeSet;%0A%0A
 /**%0A * h
@@ -293,353 +293,1619 @@
     
+T
 re
-turn sb.toString();%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem316 prob = new Problem316();%0A//        System.out.println(prob.removeDuplicateLetters(%22bcabc%22)); // abc%0A//        System.out.println(prob.removeDuplicateLetters(%22bbcaac%22)); // bac%0A        System.out.println(prob.removeDuplicateLetters(%22cbacdcbc%22)); // acdb
+eMap%3CCharacter, TreeSet%3CInteger%3E%3E map = new TreeMap%3C%3E();%0A        for (int i = 0; i %3C s.length(); i++) %7B%0A            char c = s.charAt(i);%0A            if (!map.containsKey(c)) %7B%0A                TreeSet%3CInteger%3E set = new TreeSet%3C%3E();%0A                set.add(i);%0A                map.put(c, set);%0A            %7D else %7B%0A                map.get(c).add(i);%0A            %7D%0A        %7D%0A        Set%3CCharacter%3E chars = map.keySet();%0A        while (!map.isEmpty()) %7B%0A            char resultChar = ' ';%0A            int resultIdx = -1;%0A            for (char c1 : chars) %7B%0A                TreeSet%3CInteger%3E set1 = map.get(c1);%0A                int idx = set1.first();%0A                boolean skipped = false;%0A                for (char c2 : chars) %7B%0A                    if (c1 == c2) %7B%0A                        continue;%0A                    %7D%0A                    TreeSet%3CInteger%3E set2 = map.get(c2);%0A                    int hiIdx = set2.last();%0A                    if (idx %3E hiIdx) %7B%0A                        skipped = true;%0A                        break;%0A                    %7D%0A                %7D%0A                if (!skipped) %7B%0A                    resultChar = c1;%0A                    resultIdx = idx;%0A                    break;%0A                %7D%0A            %7D%0A            if (resultIdx != -1) %7B%0A                int idx = resultIdx;%0A                sb.append(resultChar);%0A                map.remove(resultChar);%0A                for (char c3 : chars) %7B%0A                    TreeSet%3CInteger%3E set3 = map.get(c3);%0A                    set3.removeIf((e) -%3E e %3C idx);%0A                %7D%0A            %7D%0A        %7D%0A        return sb.toString();
 %0A   

Solve problem 316