Skip to content

Commit

Permalink
add: 构造有效字符串的最少插入数
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 11, 2024
1 parent 800e73f commit ec7d67f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

###

- [构造有效字符串的最少插入数](src/stack/minimum_additions_to_make_valid_string.rs) [栈, 贪心, 字符串, 动态规划]

- LeetCode 2645. 构造有效字符串的最少插入数 <https://leetcode.cn/problems/minimum-additions-to-make-valid-string>

- [删除子串后的字符串最小长度](src/stack/minimum_string_length_after_removing_substrings.rs) [栈, 字符串, 模拟]

- LeetCode 2696. 删除子串后的字符串最小长度 <https://leetcode.cn/problems/minimum-string-length-after-removing-substrings>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions src/stack/minimum_additions_to_make_valid_string.rs
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;
}
}
1 change: 1 addition & 0 deletions src/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub mod validate_stack_sequences;
pub mod remove_nodes_from_linked_list;
pub mod number_of_visible_people_in_a_queue;
pub mod minimum_string_length_after_removing_substrings;
pub mod minimum_additions_to_make_valid_string;
25 changes: 25 additions & 0 deletions tests/stack/minimum_additions_to_make_valid_string_test.rs
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);
}
1 change: 1 addition & 0 deletions tests/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub mod minimum_cost_tree_from_leaf_values_test;
pub mod remove_nodes_from_linked_list_test;
pub mod number_of_visible_people_in_a_queue_test;
pub mod minimum_string_length_after_removing_substrings_test;
pub mod minimum_additions_to_make_valid_string_test;

0 comments on commit ec7d67f

Please sign in to comment.