Skip to content

Commit ff9658e

Browse files
committed
139
1 parent 9cb5a02 commit ff9658e

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
### leetcode practise solutions in RUST
33

44
### current status
5-
176/1552 completed
5+
177/1552 completed
66

77
### Finished problems:
88
|id|Solution|
@@ -49,6 +49,7 @@
4949
|134|[Gas Station](src/_0134_gas_station.rs)|
5050
|135|[Candy](src/_0135_candy.rs)|
5151
|136|[Single Number](src/_0136_single_number.rs)|
52+
|139|[Word Break](src/_0139_word_break.rs)|
5253
|151|[Reverse Words in a String](src/_0151_reverse_words_in_a_string.rs)|
5354
|155|[Min Stack](src/_0155_min_stack.rs)|
5455
|167|[Two Sum II - Input array is sorted](src/_0167_two_sum_ii.rs)|

src/_0139_word_break.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
struct Solution;
2+
use std::collections::HashSet;
3+
4+
impl Solution {
5+
pub fn word_break(s: String, word_dict: Vec<String>) -> bool {
6+
let word_dictionary: HashSet<String> = word_dict.into_iter().collect();
7+
let mut dp: Vec<bool> = vec![false; s.len() + 1];
8+
dp[0] = true;
9+
10+
for i in 1..=s.len() {
11+
for j in 0..i {
12+
if dp[j] && word_dictionary.contains(&s[j..i]) {
13+
dp[i] = true;
14+
break;
15+
}
16+
}
17+
}
18+
return dp[s.len()];
19+
}
20+
}
21+
22+
#[test]
23+
fn test() {
24+
assert_eq!(
25+
Solution::word_break(
26+
"leetcode".to_string(),
27+
vec!["leet".to_string(), "code".to_string()]
28+
),
29+
true
30+
);
31+
assert_eq!(
32+
Solution::word_break(
33+
"catsandog".to_string(),
34+
vec![
35+
"cats".to_string(),
36+
"dog".to_string(),
37+
"sand".to_string(),
38+
"and".to_string(),
39+
"cat".to_string()
40+
]
41+
),
42+
false
43+
);
44+
assert_eq!(
45+
Solution::word_break(
46+
"applepenapple".to_string(),
47+
vec!["apple".to_string(), "pen".to_string()]
48+
),
49+
true
50+
);
51+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ mod _0135_candy;
9696
//
9797
mod _0136_single_number;
9898
//
99+
mod _0139_word_break;
100+
//
99101
mod _0151_reverse_words_in_a_string;
100102
//
101103
mod _0155_min_stack;

0 commit comments

Comments
 (0)