✅EASY C++ SOLUTION using STACK✅
PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT\n# CONNECT WITH ME\n### https://www.linkedin.com/in/pratay-nandy-9ba57b229/\n#### https://www.instagram.com/pratay_nand
# **PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT**\n# **CONNECT WITH ME**\n### **[https://www.linkedin.com/in/pratay-nandy-9ba57b229/]()**\n#### **[https://www.instagram.com/pratay_nandy/]()**\n\n# Approach\nThe provided C++ code is an implementation of the "Smallest Subsequence" problem. The goal of this problem is to remove duplicate letters from a given string while maintaining the lexicographical order of the remaining letters.\n\nHere\'s an explanation of the approach:\n\n- Create a stack st to store characters as you iterate through the input string s.\n\n- Create two vectors:\n\n1. `lastindex`: It stores the last index where each character appears in the string s. This information helps in deciding when to remove characters from the stack.\n2. `seen`: It tracks whether a character has been added to the stack or not to avoid duplicates.\n- Iterate through the string s to populate the lastindex vector. For each character, update its corresponding index in the lastindex vector.\n\nIterate through the string s again:\n\n- For each character `s[i]`, check if it has already been seen (i.e., if `seen[curr]` is true). If it has been seen, skip it.\nIf it hasn\'t been seen:\n- While the stack is not empty, the character at the top of the stack `(st.top())` is greater than the current character `s[i]`, and there\'s still a remaining occurrence of the character after the current index (i.e., `i < lastindex[st.top()-\'a\'])`, pop characters from the stack and mark them as unseen `(seen[st.top() - \'a\'] = false)` to ensure that the smaller characters appear later.\n- Push the current character`s[i]` onto the stack and mark it as seen (`seen[curr] = true`).\n- After processing all characters in the input string, the stack `st` contains the characters to keep in the lexicographical order.\n\n- Build the result string `res` by popping characters from the stack and reversing it (since the characters were pushed onto the stack in reverse order).\n\n- Return the result string as the final answer.\n\nThis approach ensures that the characters in the result string are in lexicographical order, and duplicates are removed. The order is preserved by considering the last index of each character and removing characters from the stack when necessary.\n![upvote.png](https://assets.leetcode.com/users/images/ac096b91-c068-46ab-a6dc-95b535ff71b1_1695715189.0387926.png)\n\n<!-- Describe your approach to solving the problem. -->\n\n<!-- # Complexity -->\n<!-- - Time complexity: -->\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n<!-- - Space complexity: -->\n\n# Code\n```\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n        stack<char>st;\n        vector<int>lastindex(26,0);\n        vector<bool>seen(26,false);\n        for(int i = 0 ;i<s.size();i++)\n        {\n            lastindex[s[i] - \'a\'] = i; \n        }\n        for(int i = 0 ;i<s.size();i++)\n        {\n            int curr = s[i] - \'a\'; \n            if(seen[curr])\n            {\n                continue;\n            }\n            while(st.size() > 0 && st.top() > s[i] && i < lastindex[st.top()-\'a\'])\n            {\n                seen[st.top() - \'a\'] = false;\n                st.pop();\n            }\n            st.push(s[i]);\n            seen[curr] = true;  \n        }\n        string res= "";\n        while(st.size() > 0)\n        {\n            res += st.top();\n            st.pop();\n        }\n        reverse(res.begin(),res.end());\n        return res;\n    }\n};\n```
['C++']