Searching wether the element we are removing can be removed or not?
Writing the solution so that it stays in my find and for future reference.\n# Intuition\n Describe your first thoughts on how to solve this problem. \nMontonic 
Writing the solution so that it stays in my find and for future reference.\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMontonic stack idea comes into the mind when the  core idea behind a monotonic stack is to maintain a stack of elements in such a way that the elements in the stack either strictly increase or strictly decrease (depending on the problem requirements).\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere the question demands that we have to make the ans lexicographically sorted with no duplicates.\n\nSo  we have to remove the larger number which have occured before.\nWe can only delete the larger element if its index occur after the current element.\n\n# Code\n```\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n         stack<char>st;\n         map<char,vector<int>>mp; \n         vector<int>vis(26,0);\n         for(int i  =0 ; i< s.size() ;i++)\n         {\n             mp[s[i]].push_back(i);\n         }\n        for(int i = 0 ; i < s.size() ;i++)\n        {\n            while(!st.empty()&&s[i] < st.top()&&vis[s[i]-\'a\']==0)\n            {\n                 vector<int>indices =  mp[st.top()];\n                 auto ind = upper_bound(indices.begin(),indices.end(),i);\n\n                 if(ind !=indices.end())\n                 {\n                     vis[st.top()-\'a\']=0;\n                     st.pop();\n                 }\n                 else\n                 {\n                     break;\n                 }\n            }\n            if(vis[s[i]-\'a\']==0){\n            vis[s[i]-\'a\'] =1;\n              st.push(s[i]);\n            }\n        }\n        string ans="";\n        while(!st.empty())\n        {\n            ans+=st.top();\n            st.pop();\n        }\n        reverse(ans.begin(),ans.end());\n       \n        return ans;\n    }\n};\n```
['C++']