-
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
6 changed files
with
49 additions
and
0 deletions.
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
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,18 @@ | ||
// 构造有效字符串的最少插入数 | ||
// https://leetcode.cn/problems/minimum-additions-to-make-valid-string | ||
// INLINE ../../images/stack/minimum_additions_to_make_valid_string.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn add_minimum(word: String) -> i32 { | ||
let n = word.len(); | ||
let mut cnt = 1; | ||
for i in 1..n { | ||
if word.chars().nth(i) <= word.chars().nth(i - 1) { | ||
cnt += 1; | ||
} | ||
} | ||
return cnt * 3 - n as i32; | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
tests/stack/minimum_additions_to_make_valid_string_test.rs
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,25 @@ | ||
use rust_practice::stack::minimum_additions_to_make_valid_string::Solution; | ||
|
||
#[test] | ||
fn add_minimum() { | ||
// 示例 1: | ||
// 输入:word = "b" | ||
// 输出:2 | ||
// 解释:在 "b" 之前插入 "a" ,在 "b" 之后插入 "c" 可以得到有效字符串 "abc" 。 | ||
let word = "b".to_string(); | ||
assert_eq!(Solution::add_minimum(word), 2); | ||
|
||
// 示例 2: | ||
// 输入:word = "aaa" | ||
// 输出:6 | ||
// 解释:在每个 "a" 之后依次插入 "b" 和 "c" 可以得到有效字符串 "abcabcabc" 。 | ||
let word = "aaa".to_string(); | ||
assert_eq!(Solution::add_minimum(word), 6); | ||
|
||
// 示例 3: | ||
// 输入:word = "abc" | ||
// 输出:0 | ||
// 解释:word 已经是有效字符串,不需要进行修改。 | ||
let word = "abc".to_string(); | ||
assert_eq!(Solution::add_minimum(word), 0); | ||
} |
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