Stack Solution 0(n)
From the input string, we are trying to greedily build a monotonically increasing result string. If the next character is smaller than the back of the result st
From the input string, we are trying to greedily build a monotonically increasing result string. If the next character is smaller than the back of the result string, we remove larger characters from the back providing there are more occurrences of that character later in the input string.\n\n**Time Complexity:**\nTime O(N)\nSpace O(26) i.e. constant space as We need the constant memory to track up to 26 unique letters.\n\n**Implementation**\n\n```\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n    vector<char> st;\n   \n    vector<int> ct(26,0);\n    vector<int> visi(26,0);\n    for(auto &x:s) \n        ct[x-\'a\']++;\n        \n    for(auto&x:s)\n    {   \n        ct[x-\'a\']--;\n        \n        if(visi[x-\'a\']==0)\n        {\n            while(st.size()!=0 && x < st.back() && ct[st.back()-\'a\']>0)\n            {   \n                visi[st.back() - \'a\']=0;\n                st.pop_back();\n            }\n            visi[x-\'a\']=1;\n            st.push_back(x);\n        }\n    }\n     \n    string res = "" ;\n    for(auto &x : st)\n        res += x;\n    return res;\n        \n    }\n};\n```
[]