| 1 | pub fn replace_words(dictionary: Vec<String>, sentence: String) -> String { |
| 2 | let mut dictionary = dictionary; |
| 3 | dictionary.sort_by(|a, b|a.len().cmp(&b.len())); |
| 4 | let mut ret_vec = vec![]; |
| 5 | 'outer: for word in sentence.split_ascii_whitespace() { |
| 6 | for prefix in &dictionary { |
| 7 | if word.starts_with(prefix) { |
| 8 | ret_vec.push(prefix.clone()); |
| 9 | continue 'outer; |
| 10 | } |
| 11 | } |
| 12 | ret_vec.push(word.to_string()) |
| 13 | } |
| 14 | ret_vec.join(" ") |
| 15 | } |
| 16 | |
| 17 | fn main() { |
| 18 | let dictionary = vec!["catt".to_string(), "cat".to_string(), "bat".to_string(), "rat".to_string()]; |