Simple | Clean Code | Beats 99% | 1ms | C++ | Greedy | Sorting | Brute Force
\n Describe your first thoughts on how to solve this problem. \n# Approach and Intuition\n Describe your approach to solving the problem. \nWrite Brute Force Fo
\n<!-- Describe your first thoughts on how to solve this problem. -->\n# Approach and Intuition\n<!-- Describe your approach to solving the problem. -->\nWrite Brute Force For the Given Solution.\n\nSince if we apply it over Complete string, it may lead to Time Limit Exceed. So to avoid it we will only Store Indices according to character in map and then remove them accordingly in order to reduce time.\n\nFinally Constructing the final string ans using the elements of map\n\n# Complexity\n- Time complexity: $$O(n*log(n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    string clearStars(string s) {\n        map<char,vector<int>> mp;\n        for ( int i = 0 ; i < int(s.size()) ; i++ ) {\n            if ( s[i] == \'*\' ) {\n                while ( mp.begin()->second.size() == 0 ) {\n                    mp.erase(mp.begin());\n                }\n                mp.begin()->second.pop_back();\n            }\n            else {\n                mp[s[i]].emplace_back(i);\n            }\n        }\n        string ans = "";\n        vector<char> a(int(s.size()),\'0\');\n        for ( auto i : mp ) {\n            for ( auto ind : i.second ) {\n                a[ind] = i.first;\n            }\n        }\n        for ( auto i : a ) {\n            if ( i != \'0\' ) ans += i;\n        }\n        return ans;\n    }\n};\n```\n# Please Upvote if you Liked, Understood the Solution\n\n# Thanks for Reading \uD83D\uDE0A\uD83D\uDE0A\n
['String', 'Greedy', 'Sorting', 'C++']