Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c7ebef0

Browse files
committed
word break
1 parent dfb2f42 commit c7ebef0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

‎src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
mod max_prod_subarray;
1+
mod word_break;
22
fn main() {}

‎src/word_break.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub struct Solution {}
2+
impl Solution {
3+
pub fn word_break(s: String, word_dict: Vec<String>) -> bool {
4+
let mut map = vec![false; word_dict.len() + 1];
5+
map[word_dict.len() as usize] = true;
6+
for i in (0..word_dict.len()).rev() {
7+
for word in &word_dict {
8+
if i + word.len() <= s.len() && &s[i..i + word.len()] == word {
9+
map[i] = map[i + word.len()];
10+
}
11+
if map[i] {
12+
break;
13+
}
14+
}
15+
}
16+
return map[0];
17+
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /