Easy C++ solution , explained
Here greedy approach is applied to solve the question.\nWe\'ll take the stack and a frequency vector and visited vector.\nIf the coming character is visted befo
Here greedy approach is applied to solve the question.\nWe\'ll take the stack and a frequency vector and visited vector.\nIf the coming character is visted before then we\'ll just decrease its frequency and move on to next character in string.\nWe\'ll only fill stack in lexicographically order and when we encounter lexicographically low character then we start poping out the characters from the stack so that we can maintain lexicographical order but first we check the frequency of that character so that we can;t miss that character in our ans.\nAnd after traversing whole string we just pop out the charcters from stack and store in another string.\nAnd before returning just reverse the string to get the desired ans.\n```\nstring smallestSubsequence(string text) {\n        \n        vector<bool> seen(26, false);\n        vector<int> freq(26, 0);\n\n        for(char ch : text)\n            freq[ch - \'a\']++;\n\n        stack<char> st;\n\n        for(int i=0; i<text.size(); i++){\n            char ch = text[i];\n\n            freq[ch - \'a\']--;\n            if(seen[ch - \'a\'])\n                continue;\n\n            while(st.size() != 0 && st.top() > ch && freq[st.top() - \'a\'] > 0){\n                seen[st.top() - \'a\'] = false;\n                st.pop();\n            }\n            seen[ch - \'a\'] = true;\n            st.push(ch);\n        }\n\n        string ans = "";\n        while(st.size() != 0){\n            ans += st.top();\n            st.pop();\n        }\n        reverse(ans.begin(), ans.end());\n        return ans;\n    }
['Stack']