LeetCode Entry
2452. Words Within Two Edits of Dictionary
Words in dictionary with 2 edits
2452. Words Within Two Edits of Dictionary medium substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1336
Problem TLDR
Words in dictionary with 2 edits #medium
Intuition
- We can place all single-edits of dictionary into a hash set, then check single-edits of words.
- Or just brute-force with the same time complexity
Approach
- Rust has
retain
Complexity
-
Time complexity: \(O(n^3)\)
-
Space complexity: \(O(n)\)
Code
// 28ms
fun twoEditWords(q: Array<String>, d: Array<String>) =
q.filter { q -> d.any { d -> d.indices.count { d[it] != q[it]} < 3 }}
// 1ms
pub fn two_edit_words(mut q: Vec<String>, d: Vec<String>) -> Vec<String> {
q.retain(|q| d.iter().any(|d| d.bytes().zip(q.bytes()).filter(|(d,q)|d!=q).count()<3)); q
}