LeetCode Entry

2452. Words Within Two Edits of Dictionary

22.04.2026 medium 2026 kotlin rust

Words in dictionary with 2 edits

2452. Words Within Two Edits of Dictionary medium substack youtube

22.04.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1336

Problem TLDR

Words in dictionary with 2 edits #medium

Intuition

  1. We can place all single-edits of dictionary into a hash set, then check single-edits of words.
  2. 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
    }