C++ using bitmasking and stack with detailed explainatio
\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n        // to keep count of number of each characters in string\n        vector<int> co
```\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n        // to keep count of number of each characters in string\n        vector<int> count(26);\n        \n        // bit mask to mark the characters that are already present in the stack\n        int visited=0;\n        \n        // we will use stack to keep track of characters that are less than current \n        // characters and appear in later part of the string\n        stack<int> st;\n        \n        \n        int n=s.size();\n        for (char ch:s){\n            count[ch-\'a\']++;\n        }\n        \n        \n        for (int i=0;i<n;i++){\n            // bring characters into the range 0-25\n            int ch = s[i]-\'a\';\n            \n            // decrease the count of characters\n            count[ch]--;\n            \n            // if the character is already present in the stack then we don\'t need to do anythinh\n            if (((1<<ch)&visited)>0) continue;\n\n            // if the character at the top of the stack is less than the current character and there are\n            // more of it left, i.e. it occurs later in the string then remove it\n            while (!st.empty() and st.top()>ch and count[st.top()]>0){\n                \n                // mark it univisited since no longer present in the stack\n                visited = visited & ~(1<<st.top());\n                st.pop();\n            }\n            \n            // push the characters in the stack and mark it visited\n            st.push(ch);\n            visited = visited|(1<<ch);\n        }\n        string ans="";\n        while (!st.empty()){\n            char ch = \'a\'+st.top();\n            st.pop();\n            \n            // the characters in the stack will be in reverse order of their appearance in the string\n            ans.insert(ans.begin(),ch);\n        }\n        return ans;\n    }\n};\n```
[]