File tree 3 files changed +55
-1
lines changed
3 files changed +55
-1
lines changed Original file line number Diff line number Diff line change 2
2
### leetcode practise solutions in RUST
3
3
4
4
### current status
5
- 176 /1552 completed
5
+ 177 /1552 completed
6
6
7
7
### Finished problems:
8
8
| id| Solution|
49
49
| 134| [ Gas Station] ( src/_0134_gas_station.rs ) |
50
50
| 135| [ Candy] ( src/_0135_candy.rs ) |
51
51
| 136| [ Single Number] ( src/_0136_single_number.rs ) |
52
+ | 139| [ Word Break] ( src/_0139_word_break.rs ) |
52
53
| 151| [ Reverse Words in a String] ( src/_0151_reverse_words_in_a_string.rs ) |
53
54
| 155| [ Min Stack] ( src/_0155_min_stack.rs ) |
54
55
| 167| [ Two Sum II - Input array is sorted] ( src/_0167_two_sum_ii.rs ) |
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -96,6 +96,8 @@ mod _0135_candy;
96
96
//
97
97
mod _0136_single_number;
98
98
//
99
+ mod _0139_word_break;
100
+ //
99
101
mod _0151_reverse_words_in_a_string;
100
102
//
101
103
mod _0155_min_stack;
You can’t perform that action at this time.
0 commit comments