✅✅Faster || Easy To Understand || C++ Code
Using Monotonic Stack\n\n Time Complexity :- O(N)\n\n Space Complexity :- O(N)\n\n\nclass Solution {\npublic:\n    string smallestSubsequence(string str) {\n   
* ***Using Monotonic Stack***\n\n* ***Time Complexity :- O(N)***\n\n* ***Space Complexity :- O(N)***\n\n```\nclass Solution {\npublic:\n    string smallestSubsequence(string str) {\n        \n        int n = str.size();\n        \n        // declare a stack, this will store the character in increasing order\n        \n        stack<char> st;\n        \n        vector<int> last_index(26, -1);\n        \n        // store the last index of every character\n        \n        for(int i = 0; i < n; i++)\n        {\n            last_index[str[i] - \'a\'] = i;\n        }\n        \n        // curr_set will keep track of included characters\n        \n        unordered_set<char> curr_set;\n        \n        for(int i = 0; i < n; i++)\n        {\n            // if the curr character is included then simply skip it\n            \n            if(curr_set.count(str[i]))\n            {\n                continue;\n            }\n            \n            // pop the largest character from top, if the top element is present to the right of i\n            \n            while(!st.empty() && st.top() > str[i] && last_index[st.top() - \'a\'] > i)\n            {\n                curr_set.erase(st.top());\n                \n                st.pop();\n            }\n            \n            // add the curr character\n            \n            st.push(str[i]);\n                \n            curr_set.insert(str[i]);\n        }\n        \n        // make the res array\n        \n        string res = "";\n        \n        while(st.empty() == false)\n        {\n            res += st.top();\n            \n            st.pop();\n        }\n        \n        // reverse the res\n        \n        reverse(res.begin(), res.end());\n        \n        return res;\n    }\n};\n```
['Stack', 'C', 'C++']