MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / replace_words

Function replace_words

648-replace-words.rs:1–15  ·  view source on GitHub ↗
(dictionary: Vec<String>, sentence: String)

Source from the content-addressed store, hash-verified

1pub 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
17fn main() {
18 let dictionary = vec!["catt".to_string(), "cat".to_string(), "bat".to_string(), "rat".to_string()];

Callers

nothing calls this directly

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected