Created
December 17, 2024 16:07
-
-
Save igavrysh/648177b8e70c7dce282e47f99fa254af to your computer and use it in GitHub Desktop.
2182. Construct String With Repeat Limit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
2182. Construct String With Repeat Limit | |
https://leetcode.com/problems/construct-string-with-repeat-limit | |
Medium | |
Hint | |
You are given a string s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no letter appears more than repeatLimit times in a row. You do not have to use all characters from s. | |
Return the lexicographically largest repeatLimitedString possible. | |
A string a is lexicographically larger than a string b if in the first position where a and b differ, string a has a letter that appears later in the alphabet than the corresponding letter in b. If the first min(a.length, b.length) characters do not differ, then the longer string is the lexicographically larger one. | |
Example 1: | |
Input: s = "cczazcc", repeatLimit = 3 | |
Output: "zzcccac" | |
Explanation: We use all of the characters from s to construct the repeatLimitedString "zzcccac". | |
The letter 'a' appears at most 1 time in a row. | |
The letter 'c' appears at most 3 times in a row. | |
The letter 'z' appears at most 2 times in a row. | |
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString. | |
The string is the lexicographically largest repeatLimitedString possible so we return "zzcccac". | |
Note that the string "zzcccca" is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString. | |
Example 2: | |
Input: s = "aababab", repeatLimit = 2 | |
Output: "bbabaa" | |
Explanation: We use only some of the characters from s to construct the repeatLimitedString "bbabaa". | |
The letter 'a' appears at most 2 times in a row. | |
The letter 'b' appears at most 2 times in a row. | |
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString. | |
The string is the lexicographically largest repeatLimitedString possible so we return "bbabaa". | |
Note that the string "bbabaaa" is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString. | |
Constraints: | |
1 <= repeatLimit <= s.length <= 10^5 | |
s consists of lowercase English letters. | |
*/ | |
class Solution { | |
public String repeatLimitedString(String s, int repeatLimit) { | |
int[] fq = new int[26]; | |
for (int i = 0; i < s.length(); i++) { | |
fq[s.charAt(i)-'a']++; | |
} | |
PriorityQueue<int[]> pq = new PriorityQueue<>((int[] p1, int[] p2) -> { | |
return -1*Integer.compare(p1[0], p2[0]); | |
}); | |
for (int i = 0; i < 26; i++) { | |
if (fq[i] == 0) { continue; } | |
pq.offer(new int[]{i,fq[i]}); | |
} | |
StringBuilder sb = new StringBuilder(); | |
int[] top_pair = null; | |
while (!pq.isEmpty()) { | |
int[] p = pq.poll(); | |
if (top_pair == null) { | |
int used = Math.min(repeatLimit, p[1]); | |
for (int j = 0; j < used; j++) { | |
sb.append((char)(p[0]+'a')); | |
} | |
p[1] -= used; | |
if (p[1] != 0) { | |
top_pair = p; | |
} | |
} else { | |
sb.append((char)(p[0]+'a')); | |
p[1]--; | |
if (p[1] != 0) { | |
pq.offer(p); | |
} | |
pq.offer(top_pair); | |
top_pair = null; | |
} | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment