Skip to content

Commit c98e2f7

Browse files
committed
Create 2452-words-within-two-edits-of-dictionary.rs
1 parent 1bc59b7 commit c98e2f7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
2+
let dic_chars = dictionary.iter().map(|s|s.chars().collect::<Vec<char>>())
3+
.collect::<Vec<Vec<char>>>();
4+
let mut queries = queries;
5+
queries.retain(|s|{
6+
'outer: for d_ch in &dic_chars {
7+
let mut count = 0;
8+
for (i, c) in s.chars().collect::<Vec<char>>().iter().enumerate() {
9+
if d_ch[i] != *c { count += 1 }
10+
if count > 2 { continue 'outer }
11+
}
12+
return true
13+
}
14+
return false
15+
});
16+
queries
17+
}
18+
19+
fn main() {
20+
let queries = vec!["word".to_string(), "note".to_string(),
21+
"ants".to_string(), "wood".to_string()];
22+
let dictionary = vec!["wood".to_string(),
23+
"joke".to_string(), "moat".to_string()];
24+
println!("{:?}", two_edit_words(queries, dictionary));
25+
let queries = vec!["yes".to_string()];
26+
let dictionary = vec!["not".to_string()];
27+
println!("{:?}", two_edit_words(queries, dictionary));
28+
}

0 commit comments

Comments
 (0)