🏆💢💯 Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\nC++ []\nclass Solution {\npublic:\n    string clearStars(string s) {\n        vector<vector<int>> have(26);\n        for (int i = 0; i < s.length
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/a5b828cc-3291-415b-8490-fa685171d463_1717318356.5716841.jpeg)\n![Screenshot 2024-06-02 142338.png](https://assets.leetcode.com/users/images/edd865cd-09b6-44ab-abea-35b30200aa7f_1717318435.5919673.png)\n```C++ []\nclass Solution {\npublic:\n    string clearStars(string s) {\n        vector<vector<int>> have(26);\n        for (int i = 0; i < s.length(); ++i) {\n            if (s[i] == \'*\') {\n                for (int j = 0; j < 26; ++j) {\n                    if (!have[j].empty()) {\n                        have[j].pop_back();\n                        break;\n                    }\n                }\n            } else {\n                have[s[i] - \'a\'].push_back(i);\n            }\n        }\n        vector<pair<int, char>> v;\n        for (int i = 0; i < 26; ++i) {\n            const char c = i + \'a\';\n            for (int x : have[i]) {\n                v.push_back({x, c});\n            }\n        }\n        sort(v.begin(), v.end());\n        string r;\n        for (const auto& p : v) {\n            r.push_back(p.second);\n        }\n        return r;\n    }\n};\n```\n```python3 []\nclass Solution:\n    def clearStars(self, s: str) -> str:\n        ans = \'\'\n        curr = [0 for i in range(26)]\n        for i in range(len(s)):\n            if s[i] == \'*\':\n                t = 0\n                for i in range(len(curr)):\n                    if curr[i] > 0:\n                        t = i\n                        curr[i] -= 1\n                        break\n                for j in range(len(ans) - 1, -1, -1):\n                    if ans[j] == chr(t + 97):\n                        ans = ans[0: j] + ans[j + 1: ]     \n                        break\n            else:\n                curr[ord(s[i]) - 97] += 1\n                ans += s[i]\n        return ans\n```\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:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    string clearStars(string s) {\n        vector<vector<int>> have(26);\n        for (int i = 0; i < s.length(); ++i) {\n            if (s[i] == \'*\') {\n                for (int j = 0; j < 26; ++j) {\n                    if (!have[j].empty()) {\n                        have[j].pop_back();\n                        break;\n                    }\n                }\n            } else {\n                have[s[i] - \'a\'].push_back(i);\n            }\n        }\n        vector<pair<int, char>> v;\n        for (int i = 0; i < 26; ++i) {\n            const char c = i + \'a\';\n            for (int x : have[i]) {\n                v.push_back({x, c});\n            }\n        }\n        sort(v.begin(), v.end());\n        string r;\n        for (const auto& p : v) {\n            r.push_back(p.second);\n        }\n        return r;\n    }\n};\n```
['Python', 'C++', 'Java', 'Python3']