-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0139_word_break.rs
More file actions
58 lines (54 loc) · 1.38 KB
/
s0139_word_break.rs
File metadata and controls
58 lines (54 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![allow(unused)]
pub struct Solution {}
use std::collections::HashSet;
impl Solution {
pub fn word_break(s: String, word_dict: Vec<String>) -> bool {
let n = s.len();
let mut dp = vec![false; n + 1];
dp[0] = true;
let dict = word_dict.into_iter().collect::<HashSet<String>>();
for i in 1..=n {
for j in (0..i).rev() {
if dp[j] && dict.contains(&s[j..i]) {
dp[i] = true;
break;
}
}
}
dp[n]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_139() {
assert_eq!(
Solution::word_break(
"leetcode".to_owned(),
vec!["leet".to_owned(), "code".to_owned(),]
),
true
);
assert_eq!(
Solution::word_break(
"applepenapple".to_owned(),
vec!["apple".to_owned(), "pen".to_owned(),]
),
true
);
assert_eq!(
Solution::word_break(
"catsandog".to_owned(),
vec![
"cats".to_owned(),
"dog".to_owned(),
"sand".to_owned(),
"and".to_owned(),
"cat".to_owned(),
]
),
false
);
}
}