Skip to content

Commit 7044aea

Browse files
committed
word_break追加
1 parent 8aa2139 commit 7044aea

File tree

3 files changed

+210
-2
lines changed

3 files changed

+210
-2
lines changed

src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
mod is_subsequence;
1+
mod word_break;
2+
mod word_break_rev;
23
fn main() {
3-
let result = is_subsequence::Solution::is_subsequence("abc".to_string(), "ahbgdc".to_string());
4+
let result = word_break::Solution::word_break(
5+
"leetcode".to_string(),
6+
vec!["leet".to_string(), "code".to_string()],
7+
);
48
println!("{:?}", result);
59
}

src/word_break.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
pub struct Solution;
2+
impl Solution {
3+
pub fn word_break(s: String, word_dict: Vec<String>) -> bool {
4+
let mut dp = vec![false; s.len() + 1];
5+
dp[0] = true;
6+
for i in 1..=s.len() {
7+
for j in 0..i {
8+
if dp[j] && word_dict.contains(&s[j..i].to_string()) {
9+
dp[i] = true;
10+
break;
11+
}
12+
}
13+
}
14+
dp[s.len()]
15+
}
16+
17+
pub fn word_break_rev(s: String, word_dict: Vec<String>) -> bool {
18+
let mut dp = vec![false; s.len() + 1];
19+
dp[0] = true;
20+
for i in 1..=s.len() {
21+
for j in (0..i).rev() {
22+
if dp[j] && word_dict.contains(&s[j..i].to_string()) {
23+
dp[i] = true;
24+
break;
25+
}
26+
}
27+
}
28+
dp[s.len()]
29+
}
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::*;
35+
36+
#[test]
37+
fn test_1() {
38+
assert_eq!(
39+
Solution::word_break(
40+
"leetcode".to_string(),
41+
vec!["leet".to_string(), "code".to_string()]
42+
),
43+
true
44+
);
45+
}
46+
47+
#[test]
48+
fn test_2() {
49+
assert_eq!(
50+
Solution::word_break(
51+
"applepenapple".to_string(),
52+
vec!["apple".to_string(), "pen".to_string()]
53+
),
54+
true
55+
);
56+
}
57+
58+
#[test]
59+
fn test_3() {
60+
assert_eq!(
61+
Solution::word_break(
62+
"catsandog".to_string(),
63+
vec![
64+
"cats".to_string(),
65+
"dog".to_string(),
66+
"sand".to_string(),
67+
"and".to_string(),
68+
"cat".to_string()
69+
]
70+
),
71+
false
72+
);
73+
}
74+
75+
#[test]
76+
fn test_4() {
77+
assert_eq!(
78+
Solution::word_break(
79+
"cars".to_string(),
80+
vec!["car".to_string(), "ca".to_string(), "rs".to_string()]
81+
),
82+
true
83+
);
84+
}
85+
86+
#[test]
87+
fn test_5() {
88+
assert_eq!(
89+
Solution::word_break(
90+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab".to_string(),
91+
// vec!["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]
92+
vec![
93+
"a".to_string(),
94+
"aa".to_string(),
95+
"aaa".to_string(),
96+
"aaaa".to_string(),
97+
"aaaaa".to_string(),
98+
"aaaaaa".to_string(),
99+
"aaaaaaa".to_string(),
100+
"aaaaaaaa".to_string(),
101+
"aaaaaaaaa".to_string(),
102+
"aaaaaaaaaa".to_string()
103+
]
104+
),
105+
false
106+
);
107+
}
108+
}

src/word_break_rev.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
pub struct Solution;
2+
impl Solution {
3+
pub fn word_break(s: String, word_dict: Vec<String>) -> bool {
4+
let mut dp = vec![false; s.len() + 1];
5+
dp[0] = true;
6+
7+
for i in 1..=s.len() {
8+
for j in (0..i).rev() {
9+
if dp[j] && word_dict.contains(&s[j..i].to_string()) {
10+
dp[i] = true;
11+
break;
12+
}
13+
}
14+
}
15+
16+
dp[s.len()]
17+
}
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use super::*;
23+
24+
#[test]
25+
fn test_1() {
26+
assert_eq!(
27+
Solution::word_break(
28+
"leetcode".to_string(),
29+
vec!["leet".to_string(), "code".to_string()]
30+
),
31+
true
32+
);
33+
}
34+
35+
#[test]
36+
fn test_2() {
37+
assert_eq!(
38+
Solution::word_break(
39+
"applepenapple".to_string(),
40+
vec!["apple".to_string(), "pen".to_string()]
41+
),
42+
true
43+
);
44+
}
45+
46+
#[test]
47+
fn test_3() {
48+
assert_eq!(
49+
Solution::word_break(
50+
"catsandog".to_string(),
51+
vec![
52+
"cats".to_string(),
53+
"dog".to_string(),
54+
"sand".to_string(),
55+
"and".to_string(),
56+
"cat".to_string()
57+
]
58+
),
59+
false
60+
);
61+
}
62+
63+
#[test]
64+
fn test_4() {
65+
assert_eq!(
66+
Solution::word_break(
67+
"cars".to_string(),
68+
vec!["car".to_string(), "ca".to_string(), "rs".to_string()]
69+
),
70+
true
71+
);
72+
}
73+
74+
#[test]
75+
fn test_5() {
76+
assert_eq!(
77+
Solution::word_break(
78+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab".to_string(),
79+
// vec!["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]
80+
vec![
81+
"a".to_string(),
82+
"aa".to_string(),
83+
"aaa".to_string(),
84+
"aaaa".to_string(),
85+
"aaaaa".to_string(),
86+
"aaaaaa".to_string(),
87+
"aaaaaaa".to_string(),
88+
"aaaaaaaa".to_string(),
89+
"aaaaaaaaa".to_string(),
90+
"aaaaaaaaaa".to_string()
91+
]
92+
),
93+
false
94+
);
95+
}
96+
}

0 commit comments

Comments
 (0)