[Java/C++/Python] Stack Solution O(N)
Explanation:\nFind the index of last occurrence for each character.\nUse a stack to keep the characters for result.\nLoop on each character in the input string 
## **Explanation**:\nFind the index of last occurrence for each character.\nUse a `stack` to keep the characters for result.\nLoop on each character in the input string `S`,\nif the current character is smaller than the last character in the stack,\nand the last character exists in the following stream,\nwe can pop the last character to get a smaller result.\n<br>\n\n## **Time Complexity**:\nTime `O(N)`\nSpace `O(26)`\n\n<br>\n\n**Java:**\n```java\n    public String smallestSubsequence(String S) {\n        Stack<Integer> stack = new Stack<>();\n        int[] last = new int[26], seen = new int[26];\n        for (int i = 0; i < S.length(); ++i)\n            last[S.charAt(i) - \'a\'] = i;\n        for (int i = 0; i < S.length(); ++i) {\n            int c = S.charAt(i) - \'a\';\n            if (seen[c]++ > 0) continue;\n            while (!stack.isEmpty() && stack.peek() > c && i < last[stack.peek()])\n                seen[stack.pop()] = 0;\n            stack.push(c);\n        }\n        StringBuilder sb = new StringBuilder();\n        for (int i : stack) sb.append((char)(\'a\' + i));\n        return sb.toString();\n    }\n```\n\n**C++**\n```cpp\n    string smallestSubsequence(string s) {\n        string res = "";\n        int last[26] = {}, seen[26] = {}, n = s.size();\n        for (int i = 0; i < n; ++i)\n            last[s[i] - \'a\'] = i;\n        for (int i = 0; i < n; ++i) {\n            if (seen[s[i] - \'a\']++) continue;\n            while (!res.empty() && res.back() > s[i] && i < last[res.back() - \'a\'])\n                seen[res.back() - \'a\'] = 0, res.pop_back();\n            res.push_back(s[i]);\n        }\n        return res;\n    }\n```\n\n**Python:**\n```py\n    def smallestSubsequence(self, S):\n        last = {c: i for i, c in enumerate(S)}\n        stack = []\n        for i, c in enumerate(S):\n            if c in stack: continue\n            while stack and stack[-1] > c and i < last[stack[-1]]:\n                stack.pop()\n            stack.append(c)\n        return "".join(stack)\n```\n<br>\n\n# More Good Stack Problems\nHere are some problems that impressed me.\nGood luck and have fun.\n\n- 1130. [Minimum Cost Tree From Leaf Values](https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/discuss/339959/One-Pass-O(N)-Time-and-Space)\n- 907. [Sum of Subarray Minimums](https://leetcode.com/problems/sum-of-subarray-minimums/discuss/170750/C++JavaPython-Stack-Solution)\n- 901. [Online Stock Span](https://leetcode.com/problems/online-stock-span/discuss/168311/C++JavaPython-O(1))\n- 856. [Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/discuss/141777/C++JavaPython-O(1)-Space)\n- 503. [Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/discuss/98270/JavaC++Python-Loop-Twice)\n- 496. Next Greater Element I\n- 84. Largest Rectangle in Histogram\n- 42. Trapping Rain Water\n<br>
[]