Skip to content

Commit

Permalink
构造限制重复的字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 13, 2024
1 parent 38f4b87 commit f09e044
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"aaabaaa",
"aaabbaaa",
"AAABBC",
"aababab",
"aabcbc",
"aazz",
"ababa",
Expand All @@ -35,11 +36,14 @@
"bacd",
"backgrounding",
"barcodes",
"bbabaa",
"bbabaaa",
"bdca",
"breakpad",
"cabac",
"camelcase",
"Ccodegen",
"cczazcc",
"choco",
"coverallsapp",
"Coverflow",
Expand Down Expand Up @@ -114,7 +118,9 @@
"zaaaz",
"zabcf",
"zabcfde",
"Zpanic"
"Zpanic",
"zzcccac",
"zzcccca"
],
"editor.formatOnSave": true,
"[rust]": {
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 字符串

- [构造限制重复的字符串](src/string/construct_string_with_repeat_limit.rs) [贪心, 字符串, 计数, 堆(优先队列)]

- LeetCode 2182. 构造限制重复的字符串 <https://leetcode.cn/problems/construct-string-with-repeat-limit>

- [统计重复个数](src/string/count_the_repetitions.rs) [字符串, 动态规划]

- LeetCode 466. 统计重复个数 <https://leetcode.cn/problems/count-the-repetitions>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions src/string/construct_string_with_repeat_limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 构造限制重复的字符串
// https://leetcode.cn/problems/construct-string-with-repeat-limit
// INLINE ../../images/string/construct_string_with_repeat_limit.jpeg

pub struct Solution;

impl Solution {
pub fn repeat_limited_string(s: String, repeat_limit: i32) -> String {
let mut count: [i32; 26] = [0; 26];
for c in s.chars() {
count[(c as u8 - b'a') as usize] += 1;
}
let mut ret = String::new();
let mut m = 0;
let mut i = 25;
let mut j = 24;
while i >= 0 && j >= 0 {
if count[i as usize] == 0 {
m = 0;
i -= 1;
} else if m < repeat_limit {
count[i as usize] -= 1;
ret.push((b'a' + i as u8) as char);
m += 1;
} else if j >= i || count[j as usize] == 0 {
j -= 1;
} else {
count[j as usize] -= 1;
ret.push((b'a' + j as u8) as char);
m = 0;
}
}
ret
}
}
1 change: 1 addition & 0 deletions src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ pub mod number_of_valid_clock_times;
pub mod binary_string_with_substrings_representing_1_to_n;
pub mod lexicographically_smallest_palindrome;
pub mod count_the_repetitions;
pub mod construct_string_with_repeat_limit;
38 changes: 38 additions & 0 deletions tests/string/construct_string_with_repeat_limit_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use rust_practice::string::construct_string_with_repeat_limit::Solution;

#[test]
fn repeat_limited_string() {
// 示例 1:
// 输入:s = "cczazcc", repeatLimit = 3
// 输出:"zzcccac"
// 解释:使用 s 中的所有字符来构造 repeatLimitedString "zzcccac"。
// 字母 'a' 连续出现至多 1 次。
// 字母 'c' 连续出现至多 3 次。
// 字母 'z' 连续出现至多 2 次。
// 因此,没有字母连续出现超过 repeatLimit 次,字符串是一个有效的 repeatLimitedString 。
// 该字符串是字典序最大的 repeatLimitedString ,所以返回 "zzcccac" 。
// 注意,尽管 "zzcccca" 字典序更大,但字母 'c' 连续出现超过 3 次,所以它不是一个有效的 repeatLimitedString 。
let s = "cczazcc".to_string();
let repeat_limit = 3;
assert_eq!(
Solution::repeat_limited_string(s, repeat_limit),
"zzcccac".to_string()
);

// 示例 2:
// 输入:s = "aababab", repeatLimit = 2
// 输出:"bbabaa"
// 解释:
// 使用 s 中的一些字符来构造 repeatLimitedString "bbabaa"。
// 字母 'a' 连续出现至多 2 次。
// 字母 'b' 连续出现至多 2 次。
// 因此,没有字母连续出现超过 repeatLimit 次,字符串是一个有效的 repeatLimitedString 。
// 该字符串是字典序最大的 repeatLimitedString ,所以返回 "bbabaa" 。
// 注意,尽管 "bbabaaa" 字典序更大,但字母 'a' 连续出现超过 2 次,所以它不是一个有效的 repeatLimitedString 。
let s = "aababab".to_string();
let repeat_limit = 2;
assert_eq!(
Solution::repeat_limited_string(s, repeat_limit),
"bbabaa".to_string()
);
}
1 change: 1 addition & 0 deletions tests/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub mod number_of_valid_clock_times_test;
pub mod binary_string_with_substrings_representing_1_to_n_test;
pub mod lexicographically_smallest_palindrome_test;
pub mod count_the_repetitions_test;
pub mod construct_string_with_repeat_limit_test;

0 comments on commit f09e044

Please sign in to comment.