2182. Construct String With Repeat Limit Medium

@problem@discussion
#String#Greedy#Heap (Priority Queue)#Counting



1/**
2 * [2182] Construct String With Repeat Limit
3 *
4 * 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.
5 * Return the lexicographically largest repeatLimitedString possible.
6 * 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.
7 *  
8 * Example 1:
9 * 
10 * Input: s = "cczazcc", repeatLimit = 3
11 * Output: "zzcccac"
12 * Explanation: We use all of the characters from s to construct the repeatLimitedString "zzcccac".
13 * The letter 'a' appears at most 1 time in a row.
14 * The letter 'c' appears at most 3 times in a row.
15 * The letter 'z' appears at most 2 times in a row.
16 * Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
17 * The string is the lexicographically largest repeatLimitedString possible so we return "zzcccac".
18 * 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.
19 * 
20 * Example 2:
21 * 
22 * Input: s = "aababab", repeatLimit = 2
23 * Output: "bbabaa"
24 * Explanation: We use only some of the characters from s to construct the repeatLimitedString "bbabaa". 
25 * The letter 'a' appears at most 2 times in a row.
26 * The letter 'b' appears at most 2 times in a row.
27 * Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
28 * The string is the lexicographically largest repeatLimitedString possible so we return "bbabaa".
29 * 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.
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	1 <= repeatLimit <= s.length <= 10^5
35 * 	s consists of lowercase English letters.
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/construct-string-with-repeat-limit/
41// discuss: https://leetcode.com/problems/construct-string-with-repeat-limit/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn repeat_limited_string(s: String, repeat_limit: i32) -> String {
47        String::new()
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_2182() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.