Java monotonic increasing stack solution
This approach is remembering the frequency in the string, and using a monotonic increasing stack to make sure the char in the stack is monotonic increasing in l
This approach is remembering the frequency in the string, and using a monotonic increasing stack to make sure the char in the stack is monotonic increasing in lexicographical order.\n```\nclass Solution {\n    public String smallestSubsequence(String text) {\n\t\tchar[] arr = text.toCharArray();\n        \n\t\tMap<Character, Integer> map = new HashMap<>();\n\t\tSet<Character> seen = new HashSet<>();\n        \n\t\tfor (char c : arr) {\n\t\t\tmap.put(c, map.getOrDefault(c, 0) + 1);\n\t\t}\n   \n\t\tStack<Character> stack = new Stack<>();\n        \n\t\tfor (char c : arr) {\n\t\t\t//we have seen this char\n\t\t\tif (seen.contains(c)) {\n\t\t\t\tmap.put(c, map.get(c) - 1);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// if the top char is larger than current char, we should remove it\n\t\t\twhile (!stack.isEmpty() && stack.peek() > c && map.get(stack.peek()) > 1) {\n\t\t\t\tchar temp = stack.pop();\n\t\t\t\tmap.put(temp, map.get(temp) - 1);\n\t\t\t\tseen.remove(temp);\n\t\t\t}\n\t\t\tstack.push(c);\n\t\t\tseen.add(c);\n\t\t}\n\t\n\t\tStringBuilder sb = new StringBuilder();\n\t\t//while (!stack.isEmpty()) sb.insert(0, stack.pop());\n\t\t//return sb.toString();\n\t\t\n\t\t\n\t\t//credit to @yz5548\n\t\twhile (!stack.isEmpty()) sb.append(stack.pop());\n\t\treturn sb.reverse().toString();\n    }\n}\n```
[]