#2452
Medium Algorithms Words within two edits of dictionary
Array String Trie
61.8% acceptance
Feb 25, 2026
325
25
You are given two string arrays, queries and dictionary. All words in each ar
ray comprise of lowercase English letters and have the same length. * In one edit you can take a word from queries, and change any letter in it to
any other letter. Find all words from queries that, after a maximum of two edits, equal some word from dictionary. * Return a list of all words from queries, that match with some word from dicti
onary after a maximum of two edits. Return the words in the same order they appear in queries. *
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
queries.into_iter().filter(|q| {
dictionary.iter().any(|d| {
q.bytes().zip(d.bytes()).filter(|(a, b)| a != b).count() <= 2
})
}).collect()
}
}