Skip to content

Instantly share code, notes, and snippets.

@nbdd0121
Created April 26, 2021 00:18
Show Gist options
  • Save nbdd0121/45856f6c8476d9d25178740398cc3572 to your computer and use it in GitHub Desktop.
Save nbdd0121/45856f6c8476d9d25178740398cc3572 to your computer and use it in GitHub Desktop.
impl Solution {
pub fn word_break(s: String, word_dict: Vec<String>) -> Vec<String> {
fn helper<'a>(
answer: &mut Vec<String>,
current: &mut Vec<&'a str>,
str: &str,
dict: &'a [String],
) {
if str.is_empty() {
answer.push(current.join(" "));
return;
}
for w in dict.iter() {
if str.starts_with(w) {
current.push(w);
helper(answer, current, &str[w.len()..], dict);
current.pop();
}
}
}
let mut answer = Vec::new();
helper(&mut answer, &mut Vec::new(), &s, &word_dict);
answer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment