-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathword-break-ii.rs
More file actions
23 lines (23 loc) · 792 Bytes
/
word-break-ii.rs
File metadata and controls
23 lines (23 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
impl Solution {
pub fn word_break(s: String, word_dict: Vec<String>) -> Vec<String> {
struct Sol<'a> { dict: &'a [String], ans: Vec<String>, parts: Vec<&'a str> }
impl<'a> Sol<'a> {
fn dfs(self: &mut Self, s: &str) {
if s.is_empty() {
self.ans.push(self.parts.join(" "));
return;
}
for w in self.dict.iter() {
if s.starts_with(w) {
self.parts.push(w);
self.dfs(&s[w.len()..]);
self.parts.pop();
}
}
}
}
let mut sol = Sol { dict: &word_dict, ans: vec![], parts: vec![] };
sol.dfs(&s);
sol.ans
}
}