-
Notifications
You must be signed in to change notification settings - Fork 24
/
lib.rs
33 lines (30 loc) · 828 Bytes
/
lib.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
struct Solution {}
impl Solution {
pub fn word_break(mut s: String, mut word_dict: Vec<String>) -> bool {
// 参考题解 https://leetcode-cn.com/problems/word-break/solution/dan-ci-chai-fen-ju-jue-zhuang-xcong-jian-dan-de-xi/
// 动态规划算法,dp[i]表示s前i个字符能否拆分
// 转移方程:dp[j] = dp[i] && check(s[i+1, j]);
let mut dp = vec![true; s.len() + 1];
for i in 1..s.len() + 1 {
for j in (0..i).rev() {
dp[i] = dp[j] && word_dict.contains(&s[j..i].to_string());
if dp[i] {
break;
}
}
}
println!("{:?}", dp);
dp[dp.len() - 1]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tests() {
Solution::word_break(
"leetcode".to_string(),
vec!["leet".to_string(), "code".to_string()],
);
}
}