Created
April 26, 2021 00:18
-
-
Save nbdd0121/45856f6c8476d9d25178740398cc3572 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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