Java Easy to Understand | PriorityQueue Solution
Add all the characters to a PriorityQueue which sorts in reverse order.\n2. Remove all the elements in the queue and add them to a String if they are occuring l
1. Add all the characters to a PriorityQueue which sorts in reverse order.\n2. Remove all the elements in the queue and add them to a String if they are occuring less then the limit\n3. Else push them to another PriorityQueue  and add them if there is a new character to go before this.\n\nUpvote if you like the solution.\n\n```\n    public String repeatLimitedString(String s, int repeatLimit) {\n\n        PriorityQueue<Character> q = new PriorityQueue<>(Comparator.reverseOrder());\n        PriorityQueue<Character> extra = new PriorityQueue<>(Comparator.reverseOrder());\n        for(char c : s.toCharArray()){\n            q.add(c);\n        }\n        StringBuilder sb = new StringBuilder();\n\n        char prev = q.poll();\n        sb.append(prev);\n        int count = 1;\n\n        while(!q.isEmpty()){\n            char c =  q.poll();\n            if(prev == c && count < repeatLimit) {\n                sb.append(c);\n                count++;\n            } else if (prev == c ) {\n                extra.add(c);\n            } else {\n                if (!extra.isEmpty() && extra.peek() > c){\n                    sb.append(c);\n                    int popCount =0;\n                    while (!extra.isEmpty() && extra.peek() > c && popCount < repeatLimit) {\n                        q.add(extra.poll());\n                        popCount++;\n                    }\n                } else {\n                    sb.append(c);\n                }\n                prev = c;\n                count=1;\n            }\n        }\n        return sb.toString();\n    }\n```
['Heap (Priority Queue)', 'Java']