Skip to content

Commit 3499e48

Browse files
add 2182
1 parent ed9005e commit 3499e48

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|------|----------------|---------------|--------|-------------|-------------
11+
| 2182 |[Construct String With Repeat Limit](https://leetcode.com/problems/construct-string-with-repeat-limit/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2182.java) ||Medium||
1112
| 2181 |[Merge Nodes in Between Zeros](https://leetcode.com/problems/merge-nodes-in-between-zeros/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2181.java) ||Medium||
1213
| 2180 |[Count Integers With Even Digit Sum](https://leetcode.com/problems/count-integers-with-even-digit-sum/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2180.java) ||Easy||
1314
| 2177 |[Find Three Consecutive Integers That Sum to a Given Number](https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2177.java) ||Medium||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.PriorityQueue;
6+
7+
public class _2182 {
8+
public static class Solution1 {
9+
public String repeatLimitedString(String s, int repeatLimit) {
10+
Map<Character, Integer> map = new HashMap<>();
11+
for (char c : s.toCharArray()) {
12+
map.put(c, map.getOrDefault(c, 0) + 1);
13+
}
14+
PriorityQueue<Count> heap = new PriorityQueue<Count>((a, b) -> b.c - a.c);
15+
for (char c : map.keySet()) {
16+
heap.offer(new Count(c, map.get(c)));
17+
}
18+
StringBuilder sb = new StringBuilder();
19+
while (!heap.isEmpty()) {
20+
Count curr = heap.poll();
21+
int num = Math.min(curr.num, repeatLimit);
22+
while (num > 0) {
23+
sb.append(curr.c);
24+
num--;
25+
}
26+
if (curr.num > repeatLimit) {
27+
Count newCount = new Count(curr.c, curr.num - repeatLimit);
28+
if (!heap.isEmpty()) {
29+
Count next = heap.poll();
30+
sb.append(next.c);
31+
if (next.num > 1) {
32+
heap.offer(new Count(next.c, next.num - 1));
33+
}
34+
} else {
35+
break;
36+
}
37+
heap.offer(newCount);
38+
}
39+
}
40+
return sb.toString();
41+
}
42+
43+
class Count {
44+
char c;
45+
int num;
46+
47+
public Count(char c, Integer integer) {
48+
this.c = c;
49+
this.num = integer;
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)