Skip to content

Commit 9b04291

Browse files
committed
2182. Construct String With Repeat Limit: RETRY
1 parent 0901ff8 commit 9b04291

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,3 +1649,4 @@ mod s2178_maximum_split_of_positive_even_integers;
16491649
mod s2179_count_good_triplets_in_an_array;
16501650
mod s2180_count_integers_with_even_digit_sum;
16511651
mod s2181_merge_nodes_in_between_zeros;
1652+
mod s2182_construct_string_with_repeat_limit;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
*/
38+
pub 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+
45+
impl 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)]
54+
mod tests {
55+
use super::*;
56+
57+
#[test]
58+
#[ignore]
59+
fn test_2182_example_1() {
60+
let s = "cczazcc".to_string();
61+
let repeat_limit = 3;
62+
63+
let result = "zzcccac".to_string();
64+
65+
assert_eq!(Solution::repeat_limited_string(s, repeat_limit), result);
66+
}
67+
68+
#[test]
69+
#[ignore]
70+
fn test_2182_example_2() {
71+
let s = "aababab".to_string();
72+
let repeat_limit = 2;
73+
74+
let result = "bbabaa".to_string();
75+
76+
assert_eq!(Solution::repeat_limited_string(s, repeat_limit), result);
77+
}
78+
}

0 commit comments

Comments
 (0)