Java heap | simple
```\nclass Solution {\n    public String repeatLimitedString(String s, int r) {\n        int [] f = new int[26];\n        for(int i=0;i<s.length();i++){\n      
```\nclass Solution {\n    public String repeatLimitedString(String s, int r) {\n        int [] f = new int[26];\n        for(int i=0;i<s.length();i++){\n            f[s.charAt(i)-\'a\']++; // frequency map with char count\n        }\n        PriorityQueue<int []> queue = new PriorityQueue<>((a, b) -> b[0] - a[0]); // queue ordered decending by char\n        for(int i=0;i<26;i++){\n            if(f[i] > 0){\n                queue.add(new int[]{i, f[i]});\n            }\n        }\n        StringBuilder sb = new StringBuilder(); \n        while(!queue.isEmpty()){\n            int [] c = queue.poll();\n            int x = 0;\n            char ch = (char) (c[0]+\'a\');\n            while(x++ < r && c[1]-- > 0){\n                sb.append(ch);\n            }\n            if(queue.isEmpty()){\n                break;\n            }else if(c[1] > 0){\n                int [] cn = queue.poll();\n                queue.add(c);\n                sb.append((char)(cn[0] +\'a\'));\n                if(--cn[1] > 0){\n                    queue.add(cn);\n                }\n            }\n        }\n        return sb.toString();\n    }\n}
[]