Repository navigation
Expand file tree
/
Copy pathsolution.java
More file actions
60 lines (59 loc) · 3.42 KB
/
Copy pathsolution.java
File metadata and controls
60 lines (59 loc) · 3.42 KB
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
54
55
56
57
58
59
60
// 2182. Construct String With Repeat Limit
// https://leetcode.com/problems/construct-string-with-repeat-limit/
// Medium | Java | Accepted 2026-10-12
// Runtime 17 ms | Memory 47.8 MB
class Solution {
public String repeatLimitedString(String s, int repeatLimit) {
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> Integer.compare(b, a)); //Use a prioirty queue because we want to sort the characters lexographically and always use the lexographically greatest character
int[] count = new int[26];
for(int i = 0; i<s.length(); i++)
{
count[s.charAt(i)-'a']++; //Count how many of every letter appears
}
for(int i = 0; i<26; i++)
{
if(count[i]>0) //Any letter that appears gets added to the priority queue
{
pq.add(i);
}
}
StringBuilder ans = new StringBuilder();
while(!pq.isEmpty())
{
int top = pq.poll(); //First get the top letter from the pq (the lexographically largest)
if(!pq.isEmpty()) //If the priority queue isn't empty, we want to get the second largest letter after this top one
{
int next = pq.poll();
while(count[top]>0) //To get the lexographically largest string, you need to interweave the largest and second largest letters until we run out of the largest letter to use
{
int repeat = Math.min(count[top], repeatLimit); //See how many letters we use up of the largest letter's total count
count[top]-=repeat;
ans.repeat(top+'a', repeat); //Repeat the largest letter as many times as we can
if(count[top]==0) //If the count of the largest letter is now 0, add back the second largest letter since we didn't have to use it as a reset
{
pq.add(next);
break;
}
count[next]--; //Else, we use up 1 of the second largest letter (we use up just 1 because we want to reset the total amount of largest letters in the row by breaking the streak with this 1 second largest letter)
//Only use 1 so we can save more second largest letters for later
ans.append((char)(next+'a'));
if(count[next]==0) //If we run out of the second largest letter
{
if(pq.isEmpty()) //If there are no more letters available, we add the largest letter back onto the pq since it's count isn't 0
{
pq.add(top);
break;
}
next = pq.poll(); //Else, we set the second largest letter to the next value on the heap
}
}
//After we can't add any more of the largest letter to the heap (either we used all of the largest letters available or we ran out of reset letters) we repeat the process with the new largest letter on the heap
}
else //If there's only just one letter on the heap, then just add as many of this letter as we can, and then don't add it back to the heap since we can't do anything else
{
ans.repeat(top+'a', Math.min(repeatLimit, count[top]));
}
}
return ans.toString(); //Return the answer as a string
}
}