1ms, beats 100% JAVA solution with explanation
\n// solution is pretty simple and straight forward\n// first we count the frequency of each char in the string\n// then we put each char of the string in the s
```\n// solution is pretty simple and straight forward\n// first we count the frequency of each char in the string\n// then we put each char of the string in the string res(the string that we\'ll be returning)\n// if we find a char x (in text) that is smaller than the last char in our res string\n// then we remove the char from the end of the res string\n// then we will add the char x at the end of our res string\n// this will ensure that the res will be lexicographically smallest subsequence of text\n\nclass Solution {\n    public String smallestSubsequence(String text) {\n        int[] freq = new int[26];\n        boolean[] taken = new boolean[26];\n        \n        for(char c: text.toCharArray()) freq[c - \'a\']++;\n        \n        StringBuilder res = new StringBuilder();\n        res.append(\'0\');\n        \n        for(char c: text.toCharArray()) {\n            freq[c-\'a\']--;\n            if(!taken[c - \'a\']) {\n                while(res.charAt(res.length() - 1) > c && freq[res.charAt(res.length() - 1) - \'a\'] > 0) {\n                    taken[res.charAt(res.length() - 1) - \'a\'] = false;\n                    res.deleteCharAt(res.length() - 1);\n                }\n                res.append(c);\n                taken[c-\'a\'] = true;\n            }\n        }\n        \n        return res.substring(1).toString();\n    }\n}\n```
['Java']