forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2182.java
53 lines (49 loc) · 1.7 KB
/
_2182.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
public class _2182 {
public static class Solution1 {
public String repeatLimitedString(String s, int repeatLimit) {
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
PriorityQueue<Count> heap = new PriorityQueue<Count>((a, b) -> b.c - a.c);
for (char c : map.keySet()) {
heap.offer(new Count(c, map.get(c)));
}
StringBuilder sb = new StringBuilder();
while (!heap.isEmpty()) {
Count curr = heap.poll();
int num = Math.min(curr.num, repeatLimit);
while (num > 0) {
sb.append(curr.c);
num--;
}
if (curr.num > repeatLimit) {
Count newCount = new Count(curr.c, curr.num - repeatLimit);
if (!heap.isEmpty()) {
Count next = heap.poll();
sb.append(next.c);
if (next.num > 1) {
heap.offer(new Count(next.c, next.num - 1));
}
} else {
break;
}
heap.offer(newCount);
}
}
return sb.toString();
}
class Count {
char c;
int num;
public Count(char c, Integer integer) {
this.c = c;
this.num = integer;
}
}
}
}