EASY||4 STEPS DETAIL APPROACH || SAME AS 316
Intuition\n Describe your first thoughts on how to solve this problem. \nWe can come up with the idea that for lexicographic we need a stack maybe but here the 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can come up with the idea that for lexicographic we need a stack maybe but here the problem arises in removing , how do you know that the elements you are removing from stack are present later in string , might be the case that they are not present later. Also , considering the thing that it should not occur more than once , for this we will use a array and not a hashmap as it gets size extensive as we are looking up for alphabets and they are limited from 0 to 25 in the array only. \nWe will make a array which will initially store -1, later we upvate it with last occurence of the array indexed alphabets. So that while you remove from the stack you know if it is later present also or not.\n# LOOK FOR 1081 ON LEETCODE , THIS CODE THERE AS WELL WITHOUT MAKING ANY CHANGES.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialization:\n\nvisit[]: Array to track visited characters (0 for unvisited, 1 for visited).\nlastOccurence[]: Array to store the last occurrence index of each character.\nst: A stack to maintain the current result string in reverse order.\n2. Record Last Occurrences:\n\nIterate through s and store the last occurrence index of each character in lastOccurence[].\n3. Process Characters:\n\nIterate through s again:\nFor each character letter:\nWhile the stack is not empty, the top character is lexicographically greater than letter, and letter has a later occurrence:\nPop the top character from the stack (it\'s safe to remove).\nMark it as unvisited in visit[].\nIf letter hasn\'t been visited yet:\nPush it onto the stack.\nMark it as visited in visit[].\n4. Build Result:\n\nReverse the stack\'s contents to get the correct order and construct the result string.\nReturn Result:\n\n# Return the constructed string.UPVOTE\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nLINEAR\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nCONST.\n# Code\n```\nclass Solution {\n    public String smallestSubsequence(String s) {\n        int visit[]=new int [26];\n        int lastOccurence []=new int [26];\n        Stack <Character> st=new Stack<>();\n        for(int i=0;i<26;i++){\n            lastOccurence [i]=-1;\n        }\n        for(int i=0;i<s.length();i++){\n            lastOccurence[s.charAt(i)-\'a\']=i;\n        }\n        for(int i=0;i<s.length();i++){\n            char letter=s.charAt(i);\n            while(!st.isEmpty() && st.peek()>letter && lastOccurence[st.peek()-\'a\']>i && visit[letter-\'a\']==0){\n                visit[st.peek()-\'a\']=0;\n                st.pop();\n            }\n            if(visit[letter-\'a\']==0){\n                st.push(letter);\n                visit[letter-\'a\']=1;\n            }\n        }\n        StringBuilder ans=new StringBuilder("");\n        while(!st.isEmpty()){\n            ans.append(st.pop());\n        }\n        String originalString = ans.toString();\n        return reverseString(originalString);\n    }\n    private static String reverseString(String str) {\n        return new StringBuilder(str).reverse().toString();\n    }\n}\n\n```
['Stack', 'Java']