📌Beats 100% users in python || Easy to Understand ✔|| C++📌 || Java 📌|| JavaScript📌
Intuition\n Describe your first thoughts on how to solve this problem. \nVery clear intuition is do what it asks for. You will get accepted.\n\n# Approach\n Des
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nVery clear intuition is do what it asks for. You will get accepted.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirstly you need to keep track of the smallest character with largest index till the current \'star\'. This can be easily done using map data structure, create a map of char and vector in which put indexes of the current character at the back so the closest one is the last element of the vector of that char. Because we are using map data structure so it automatically sorts the characters in it. So just by accessing the first characters last index does the work. Now mark the indexes of \'star\' and the characters index.\nNow iterate through the string and create your answer by not taking the indexes you marked.\n\n# Complexity\n- Time complexity: $$O(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 in C++, Java, Python and JavaSript\n```C++ []\nclass Solution {\npublic:\n    string clearStars(string s) {\n        map<char,vector<int>> mp;\n        int n= s.size();\n        vector<int> v(n,0);\n        for(int i =0 ;i <s.size(); i++){\n            if(s[i]!=\'*\'){\n                mp[s[i]].push_back(i);\n            }\n            else{\n                v[i] = 1;\n                for(auto &x : mp){\n                    int m = x.second.size();\n                    v[x.second[m-1]] = 1;\n                    x.second.pop_back();\n                    if(x.second.size()==0) mp.erase(x.first);\n                    break;\n                }\n            }\n        }\n        string ans ="";\n        for(int i = 0; i<n; i++){\n            if(v[i]!=1) ans+=s[i];\n        }\n        return ans;\n    }\n};\n```\n```Java []\nclass Solution {\n    public String clearStars(String s) {\n        TreeMap<Character, List<Integer>> mp = new TreeMap<>();\n        int n = s.length();\n        int[] v = new int[n];\n        \n        for (int i = 0; i < n; i++) {\n            if (s.charAt(i) != \'*\') {\n                mp.computeIfAbsent(s.charAt(i), k -> new ArrayList<>()).add(i);\n            } else {\n                v[i] = 1;\n                for (Map.Entry<Character, List<Integer>> entry : mp.entrySet()) {\n                    List<Integer> indices = entry.getValue();\n                    int m = indices.size();\n                    v[indices.get(m - 1)] = 1;\n                    indices.remove(m - 1);\n                    if (indices.isEmpty()) {\n                        mp.remove(entry.getKey());\n                    }\n                    break;\n                }\n            }\n        }\n        \n        StringBuilder ans = new StringBuilder();\n        for (int i = 0; i < n; i++) {\n            if (v[i] != 1) {\n                ans.append(s.charAt(i));\n            }\n        }\n        return ans.toString();\n    }\n}\n```\n```python []\nclass Solution(object):\n    def clearStars(self, s):\n        # Using defaultdict to store the indices of each character\n        mp = defaultdict(list)\n        n = len(s)\n        v = [0] * n\n\n        for i in range(n):\n            if s[i] != \'*\':\n                mp[s[i]].append(i)\n            else:\n                v[i] = 1\n                # Sort the dictionary by key\n                sorted_mp = OrderedDict(sorted(mp.items()))\n                for key in list(sorted_mp.keys()):\n                    idx_list = sorted_mp[key]\n                    v[idx_list[-1]] = 1\n                    idx_list.pop()\n                    if not idx_list:\n                        del mp[key]\n                    break\n\n        ans = ""\n        for i in range(n):\n            if v[i] != 1:\n                ans += s[i]\n\n        return ans\n        \n```\n```JavaScript []\n/**\n * @param {string} s\n * @return {string}\n */\nvar clearStars = function(s) {\n    const mp = new Map();\n        const n = s.length;\n        const v = new Array(n).fill(0);\n\n        for (let i = 0; i < n; i++) {\n            if (s[i] !== \'*\') {\n                if (!mp.has(s[i])) {\n                    mp.set(s[i], []);\n                }\n                mp.get(s[i]).push(i);\n            } else {\n                v[i] = 1;\n                const sortedEntries = Array.from(mp.entries()).sort((a, b) => a[0].localeCompare(b[0]));\n                for (let [key, indices] of sortedEntries) {\n                    const m = indices.length;\n                    v[indices[m - 1]] = 1;\n                    indices.pop();\n                    if (indices.length === 0) {\n                        mp.delete(key);\n                    }\n                    break;\n                }\n            }\n        }\n\n        let ans = "";\n        for (let i = 0; i < n; i++) {\n            if (v[i] !== 1) {\n                ans += s[i];\n            }\n        }\n        return ans;\n};\n```\n![upvoting image.jpeg](https://assets.leetcode.com/users/images/d9d1ccc8-1b2e-404e-963c-2707067544d1_1717302529.8161004.jpeg)\n
['Python', 'C++', 'Java', 'Python3', 'JavaScript']