package leetcode;

/**
 * https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
 */
public class Problem1081 {
    public String smallestSubsequence(String text) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem1081 prob = new Problem1081();
        System.out.println(prob.smallestSubsequence("cdadabcc")); // "adbc"
        System.out.println(prob.smallestSubsequence("abcd")); // "abcd"
        System.out.println(prob.smallestSubsequence("ecbacba")); // "eacb"
        System.out.println(prob.smallestSubsequence("leetcode")); // "letcod"
        System.out.println(prob.smallestSubsequence("acbabc")); // "abc"
    }
}

@@ -12,16 +12,41 @@
 tcode;%0A%0A
+import java.util.Stack;%0A%0A
 /**%0A * h
@@ -215,508 +215,773 @@
     
-// TODO%0A        return null
+Stack%3CInteger%3E stack = new Stack%3C%3E();%0A        int%5B%5D last = new int%5B26%5D
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1081 prob = new Problem1081();%0A        System.out.println(prob.smallestSubsequence(%22cdadabcc%22)); // %22adbc%22%0A        System.out.println(prob.smallestSubsequence(%22abcd%22)); // %22abcd%22%0A        System.out.println(prob.smallestSubsequence(%22ecbacba%22)); // %22eacb%22%0A        System.out.println(prob.smallestSubsequence(%22leetcode%22)); // %22letcod%22%0A        System.out.println(prob.smallestSubsequence(%22acbabc%22)); // %22abc%22
+boolean%5B%5D visited = new boolean%5B26%5D;%0A        for (int i = 0; i %3C text.length(); i++) %7B%0A            last%5Btext.charAt(i) - 'a'%5D = i;%0A        %7D%0A        for (int i = 0; i %3C text.length(); i++) %7B%0A            int c = text.charAt(i) - 'a';%0A            if (visited%5Bc%5D) %7B%0A                continue;%0A            %7D%0A            while (!stack.isEmpty() && stack.peek() %3E c && i %3C last%5Bstack.peek()%5D) %7B%0A                visited%5Bstack.pop()%5D = false;%0A            %7D%0A            stack.push(c);%0A            visited%5Bc%5D = true;%0A        %7D%0A        StringBuilder answer = new StringBuilder();%0A        for (int i : stack) %7B%0A            answer.append((char)('a' + i));%0A        %7D%0A        return answer.toString();
 %0A   

Solve problem 1081