🔥Beats 100% | EASY | Clean Code | Stack | C++ |
Code\n\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n        stack<char> st;\n        unordered_map<char, int> last_occ;\n        unor
# Code\n```\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n        stack<char> st;\n        unordered_map<char, int> last_occ;\n        unordered_set<char> visit;\n\n        for (int i = 0; i < s.size(); i++)\n        {\n            last_occ[s[i]] = i;\n        }\n\n        for (int i = 0; i < s.size(); i++)\n        {\n            char c = s[i];\n            if (!visit.count(c))\n            {\n                while (!st.empty() && c < st.top() && i < last_occ[st.top()])\n                {\n                    visit.erase(st.top());\n                    st.pop();\n                }\n                st.push(c);\n                visit.insert(c);\n            }\n        }\n\n        string res =  "";\n        while (!st.empty())\n        {\n            res = st.top() + res;\n            st.pop();\n        }\n\n        return res;\n    }\n};\n```
['String', 'Stack', 'Monotonic Stack', 'C++']