C++ || Priority Queue || Custom Comparator
Connect with me on LinkedIn: https://www.linkedin.com/in/abhay5349singh/\n\nApproach:\n build custom comparator based on given requirements\n\t if 2 char are sa
**Connect with me on LinkedIn**: https://www.linkedin.com/in/abhay5349singh/\n\n**Approach:**\n* build custom comparator based on given requirements\n\t* if 2 char are same, we will delete char present at max index \n\t* for distinct char, we will delete lestfmost smallest\n* maintaining `priority queue` to access most suitable char for deletion at top\n\n```\nclass Solution {\npublic:\n    \n    struct node{\n        char ch;\n        int idx;\n    };\n    \n    struct compare{\n        bool operator()(const node &a, const node &b){\n            if(a.ch == b.ch) return a.idx < b.idx; // max heap\n            return a.ch > b.ch; // min heap\n        }\n    };\n    \n    string clearStars(string s) {\n        int n = s.length();\n        \n        priority_queue<node,vector<node>,compare> pq;\n        for(int i=0;i<n;i++){\n            char ch = s[i];\n            if(ch==\'*\'){\n                if(pq.size() > 0) pq.pop();\n            }else{\n                pq.push({ch, i});\n            }\n        }\n        \n        vector<pair<int,char>> v;\n        while(pq.size() > 0){\n            node cur = pq.top();\n            pq.pop();\n            \n            v.push_back({cur.idx, cur.ch});\n        }\n\t\t\n        sort(v.begin(), v.end()); // since we need to maintain orginal indexing of chars in string\n        \n        string ans = "";\n        for(pair<int,char> p : v) ans += p.second;\n        \n        return ans;\n    }\n};\n```\n\n**Do upvote if it helps :)**
[]