An TLE backtrack solution
Brute force solution with backtrack, spent an hour on this solution back in contest, finally did not worked out.\nThe algorithm works well except time complexit
Brute force solution with backtrack, spent an hour on this solution back in contest, finally did not worked out.\nThe algorithm works well except time complexity is too high when input is long.\nAround half of cases passed.\nPut here to provide another idea.\n```\n\tString rst = "zzzzzzzzzzzzzzzzzzzzzzz";\n\n\tpublic String smallestSubsequence(String text) {\n\t\tMap<Character, Integer> chars = new HashMap<>();\n\t\tchar[] t = text.toCharArray();\n\t\tfor (char c : t) {\n\t\t\tint freq = chars.getOrDefault(c, 0);\n\t\t\tchars.put(c, freq + 1);\n\t\t}\n\t\tbacktrack(t, 0, chars, "");\n\t\treturn rst;\n\t}\n\n\tvoid backtrack(char[] t, int start, Map<Character, Integer> chars, String curt) {\n\t\tif (curt.length() == chars.size()) {\n\t\t\trst = rst.compareTo(curt) > 0 ? curt : rst;\n\t\t\treturn;\n\t\t}\n\t\tfor (int i = start; i < t.length; ++i) {\n\t\t\tint freq = chars.get(t[i]);\n\t\t\tString next = curt + t[i];\n\t\t\tif (freq > 0 && next.compareTo(rst) < 0) {\n\t\t\t\tchars.put(t[i], -1);\n\t\t\t\tbacktrack(t, i + 1, chars, curt + t[i]);\n\t\t\t\tchars.put(t[i], freq);\n\t\t\t}\n\t\t}\n\t}\n```
[]