java solution using stack with comments
Detail can be found here->\nhttps://leetcode.com/problems/remove-duplicate-letters/discuss/1860486/java-solution-using-stack-with-comments\n\n\n public String s
Detail can be found here->\nhttps://leetcode.com/problems/remove-duplicate-letters/discuss/1860486/java-solution-using-stack-with-comments\n\n```\n public String smallestSubsequence(String s) {\n\t\t// hold last index of every character.\n\t\tint[] lastIndex = new int[26];\n\t\t// visited array to store every char value.\n\t\tboolean[] visited = new boolean[26];\n\t\t// hold the answer sub sequence.\n\t\tDeque<Character> ansStack = new LinkedList<>();\n\t\t// prepare last index of every character\n\t\tfor (int i = 0; i < s.length(); i++) {\n\t\t\tlastIndex[s.charAt(i) - \'a\'] = i;\n\t\t}\n\t\t// iterate each char of String\n\t\tfor (int i = 0; i < s.length(); i++) {\n\t\t\tchar ch = s.charAt(i);\n\t\t\t// if current char already visited don\'t do anything\n\t\t\tif (visited[ch - \'a\']) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// check if top of stack > current char. and if that top element of stack is\n\t\t\t// repeating later.\n\t\t\twhile (!ansStack.isEmpty() && ansStack.peek() > ch && lastIndex[ansStack.peek() - \'a\'] > i) {\n\t\t\t\t// remove top element from stack as we have smaller lexicographic string\n\t\t\t\t// combination available.\n\t\t\t\t// also make visited of top element to false as we need to read it again latter\n\t\t\t\t// for placement.\n\t\t\t\tvisited[ansStack.pop() - \'a\'] = false;\n\t\t\t}\n\t\t\t// set visited of current char true and add curr char to answer stack\n\t\t\tvisited[ch - \'a\'] = true;\n\t\t\tansStack.push(ch);\n\t\t}\n\n\t\tStringBuilder sb = new StringBuilder();\n\t\t// now we need to get elements from stack and add it to the stringBuilder\n\t\twhile (!ansStack.isEmpty()) {\n\t\t\tsb.append(ansStack.pop());\n\t\t}\n\t\t// since element are in reverse order. just reverse the elements.\n\t\treturn sb.reverse().toString();\n\t}\n```
['Stack', 'Java']