beats 100% user with cpp with TC explaination
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 
# 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:\na bit of confusion that may arise is that there are nested for loop \nso time complexity should be quadratic.If this question arises in your mind then you are on right path. So, let\'s clear the confusion , \nhere the ans can be of length 26 at most ... so the while loop which is poping the element from ans can run for at most 26 time .. \nso time complexity will be 26*n but in big O notaion we don\'t consider constant that\'s why at most places you will find the TC \nfor this will be O(n).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    string smallestSubsequence(string s) {\n    int n = s.size();\n    string ans = "";\n    vector<bool> taken(27 , false);\n    vector<int> lastOcc(27 , 0);\n    for(int i = 0 ; i< n ; i++){\n        lastOcc[s[i] - \'a\'] = i ; \n    }\n    for(int i = 0 ; i< n ; i++){\n        if(taken[s[i] - \'a\']) continue;\n        if(ans.size()== 0 || ans.back() <s[i]){\n            ans+= s[i];\n            taken[s[i] - \'a\'] = true;\n        }\n        else{\n            while(ans.size()>0 && ans.back()>s[i] && lastOcc[ans.back()-\'a\'] > i){\n             \n                taken[ans.back()-\'a\'] = false;\n                   ans.pop_back() ;\n            }\n          ans+= s[i];\n          taken[s[i] - \'a\'] = true;\n        }\n    }\n    return ans;\n    // map<char,int> mp;\n    // for(auto t: s){\n    //     mp[t]++;\n    // }\n    // string ans = "";\n    // int fr =0 ;\n    // for(int i = 0 ; i< n; i++){\n    //     if(mp[s[i]] == 0) continue;\n    //     if(mp[s[i]] ==1){\n    //         mp[s[i]]--;\n    //         cout<<s[i]<<endl;\n    //         ans += s[i];\n    //     }else{\n    //         int j = i+1 ; \n    //         map<char,int> dp = mp;\n    //         int cnt = i ; \n    //         char c = s[i];\n    //         dp[s[j]]--;\n    //         while(dp[s[j]] !=1  && j<n){\n    //             cout<<j<<" "<<dp[s[j]]<<" "<<s[j]<<endl;\n    //             if(s[j]<c && dp[s[j]]  >1 )\n    //             {\n    //                 cnt = j ; \n    //                 c = s[j];\n    //             }\n    //             dp[s[j]]--;\n    //             j++;\n    //         }\n    //         if(j<n){\n    //             if(c>s[j]){\n    //                 cout<<s[j]<<" "<<j<<endl;\n    //                 cnt = j ; \n    //                 c = s[j];\n    //             }\n    //         }\n    //         while(i<cnt ){\n    //             mp[s[i]]--;\n    //             i++;\n    //         }\n    //         ans+=c;\n    //         mp[c] = 0 ;\n    //       cout<<i<<" "<<c<<endl;\n    //       cout<<fr<<endl;\n    //       fr++;\n    //     }\n    // }\n    // return ans;\n    }\n};\n```
['C++']