-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
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 | ||
} | ||
} |
This file contains 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
This file contains 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
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() | ||
); | ||
} |
This file contains 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