Skip to content

Commit

Permalink
add: 字符串中的额外字符
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 9, 2024
1 parent d2a958d commit 432568b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"lcci",
"lcov",
"leetcode",
"leetscode",
"maxfreq",
"maxnum",
"minnum",
Expand All @@ -90,6 +91,7 @@
"RUSTDOCFLAGS",
"RUSTFLAGS",
"rustup",
"sayhelloworld",
"strs",
"subarray",
"subarrays",
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

###

- [字符串中的额外字符](src/tree/extra_characters_in_a_string.rs) [字典树, 数组, 哈希表, 字符串, 动态规划]

- LeetCode 2707. 字符串中的额外字符 <https://leetcode.cn/problems/extra-characters-in-a-string>

- [从二叉搜索树到更大和树](src/tree/binary_search_tree_to_greater_sum_tree.rs) [树, 深度优先搜索, 二叉搜索树, 二叉树]

- LeetCode 1038. 从二叉搜索树到更大和树 <https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree>
Expand Down
Binary file added images/tree/extra_characters_in_a_string.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions src/tree/extra_characters_in_a_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::collections::HashMap;
// 字符串中的额外字符
// https://leetcode.cn/problems/extra-characters-in-a-string
// INLINE ../../images/tree/extra_characters_in_a_string.jpeg

pub struct Solution;

impl Solution {
pub fn min_extra_char(s: String, dictionary: Vec<String>) -> i32 {
let n = s.len();
let mut d = vec![std::i32::MAX; n + 1];
let mut mp: HashMap<String, i32> = HashMap::new();

for word in dictionary {
*mp.entry(word).or_insert(0) += 1;
}

d[0] = 0;

for i in 1..=n {
d[i] = d[i - 1] + 1;

for j in (0..i).rev() {
if mp.contains_key(&s[j..i]) {
d[i] = d[i].min(d[j]);
}
}
}

d[n]
}
}
1 change: 1 addition & 0 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub mod maximum_sum_bst_in_binary_tree;
pub mod time_needed_to_inform_all_employees;
pub mod kth_ancestor_of_a_tree_node;
pub mod binary_search_tree_to_greater_sum_tree;
pub mod extra_characters_in_a_string;
24 changes: 24 additions & 0 deletions tests/tree/extra_characters_in_a_string_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use rust_practice::tree::extra_characters_in_a_string::Solution;

#[test]
fn min_extra_char() {
// 示例 1:
// 输入:s = "leetscode", dictionary = ["leet","code","leetcode"]
// 输出:1
// 解释:将 s 分成两个子字符串:下标从 0 到 3 的 "leet" 和下标从 5 到 8 的 "code" 。只有 1 个字符没有使用(下标为 4),所以我们返回 1 。
let s = "leetscode".to_string();
let dictionary = vec![
"leet".to_string(),
"code".to_string(),
"leetcode".to_string(),
];
assert_eq!(Solution::min_extra_char(s, dictionary), 1);

// 示例 2:
// 输入:s = "sayhelloworld", dictionary = ["hello","world"]
// 输出:3
// 解释:将 s 分成两个子字符串:下标从 3 到 7 的 "hello" 和下标从 8 到 12 的 "world" 。下标为 0 ,1 和 2 的字符没有使用,所以我们返回 3 。
let s = "sayhelloworld".to_string();
let dictionary = vec!["hello".to_string(), "world".to_string()];
assert_eq!(Solution::min_extra_char(s, dictionary), 3);
}
1 change: 1 addition & 0 deletions tests/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub mod insufficient_nodes_in_root_to_leaf_paths_test;
pub mod delete_nodes_and_return_forest_test;
pub mod kth_ancestor_of_a_tree_node_test;
pub mod binary_search_tree_to_greater_sum_tree_test;
pub mod extra_characters_in_a_string_test;

0 comments on commit 432568b

Please sign in to comment.