-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathword-break.rs
36 lines (29 loc) · 954 Bytes
/
word-break.rs
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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
/// dp[i]表示s[0..i]是否满足
/// dp[j] 是否满足取决于dp[0] —> dp[j-n] 和 s[j-n..j]是否满足
pub fn word_break(s: String, word_dict: Vec<String>) -> bool {
let mut n = s.len();
let hash = word_dict
.iter()
.map(|x| &x[..])
.collect::<std::collections::HashSet<&str>>();
let mut dp = vec![false; n];
dp[0] = hash.contains(&s[0..1]);
for i in 1..n {
if (dp[i - 1] && hash.contains(&s[i..i + 1])) || hash.contains(&s[0..i + 1]) {
dp[i] = true;
continue;
}
for j in 0..i {
dp[i] = dp[j] && hash.contains(&s[j + 1..i + 1]);
if dp[i] {
break;
}
}
}
dp[dp.len() - 1]
}
}