C++ Easy 0ms faster || stack
\n\n# Code\n\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n        vector<int> last(26,-1);\n        int n=s.length();\n        for(in
\n\n# Code\n```\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n        vector<int> last(26,-1);\n        int n=s.length();\n        for(int i=0;i<n;i++){\n            last[s[i]-\'a\']=i;\n        }\n        vector<bool> visited(26,false);\n        stack<char> st;\n        for(int i=0;i<n;i++){\n            if(visited[s[i]-\'a\'])\n            continue;\n            while(!st.empty() && s[i]<st.top() && last[st.top()-\'a\']>i){               \n                visited[st.top()-\'a\']=false;\n                 st.pop();\n            }            \n            st.push(s[i]);\n            visited[s[i]-\'a\']=true; \n        }\n        string ans="";\n        while(!st.empty()){\n        ans=st.top()+ans;\n        st.pop();\n        }\n        return ans;\n    }\n};\n```
['Stack', 'Greedy', 'C++']