same question as leetcode 316
 # Intuition  \n\n\n\n\n\n# Complexity\n- Time complexity: O(N)\n\n\n- Space complexity: O(1)\n\n\n# Code\ncpp []\nclass Solution {\npublic:\n    string smalles
<!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n        vector<int> freq(26, 0); // Frequency array to count occurrences\n        vector<bool> inStack(26, false); // Tracks characters in the stack\n        stack<char> st; // Stack to build the result\n\n        // Step 1: Count the frequency of each character in the string\n        for (char c : s) {\n            freq[c - \'a\']++;\n        }\n\n        // Step 2: Iterate through the string\n        for (char c : s) {\n            freq[c - \'a\']--; // Decrease the frequency count for the current character\n\n            // If the character is already in the stack, skip it\n            if (inStack[c - \'a\']) continue;\n\n            // Step 3: Maintain the stack in lexicographical order\n            // While the stack is not empty and the top character is greater than the current character\n            // and the top character will appear again later, pop the stack\n            while (!st.empty() && st.top() > c && freq[st.top() - \'a\'] > 0) {\n                inStack[st.top() - \'a\'] = false; // Mark the top character as not in the stack\n                st.pop(); // Pop the top character\n            }\n\n            // Step 4: Push the current character onto the stack\n            st.push(c);\n            inStack[c - \'a\'] = true; // Mark the current character as in the stack\n        }\n\n        // Step 5: Build the result from the stack\n        string result = "";\n        while (!st.empty()) {\n            result += st.top(); // Add the top character to the result\n            st.pop(); // Pop the top character\n        }\n\n        // Reverse the result string to get the correct order\n        reverse(result.begin(), result.end());\n        return result; // Return the final result\n    \n    }\n};\n```
['String', 'Stack', 'Greedy', 'Monotonic Stack', 'C++']