Java | Stack | Greedy | O(n) Time and Memory
\nclass Solution {\n    public String smallestSubsequence(String text) {\n        StringBuilder sb = new StringBuilder(); \n        Stack<Character> st = new St
```\nclass Solution {\n    public String smallestSubsequence(String text) {\n        StringBuilder sb = new StringBuilder(); \n        Stack<Character> st = new Stack<>();\n        boolean[] visited = new boolean[26]; \n        int[] counts = new int[26];\n        for(char ch : text.toCharArray()) counts[ch-\'a\']++; \n        for(char ch : text.toCharArray()) {\n            counts[ch-\'a\']--; \n            if(visited[ch-\'a\']) continue; \n            while(!st.isEmpty() && st.peek() > ch && counts[st.peek()-\'a\'] > 0) {     \n                visited[st.pop()-\'a\'] = false;\n            }\n            st.push(ch); \n            visited[ch-\'a\'] = true;\n        }\n        while(!st.isEmpty()) sb.append(st.pop()); \n        sb.reverse(); \n        return new String(sb); \n    }\n}\n```
[]