-
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
64 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
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,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] | ||
} | ||
} |
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,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); | ||
} |
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