Words Within Two Edits of Dictionary - Problem

You're given two string arrays: queries and dictionary. All words in both arrays consist of lowercase English letters and have the same length.

In one edit operation, you can take any word from queries and change exactly one letter to any other letter. Your task is to find all words from queries that can be transformed to match some word in the dictionary using at most two edits.

Goal: Return a list of all words from queries that match with some dictionary word after a maximum of two edits. Preserve the original order from queries.

Example: If queries = ["word", "note"] and dictionary = ["world", "note"], then "word" needs 1 edit to become "world", and "note" needs 0 edits (exact match).

Input & Output

example_1.py โ€” Basic Example
$ Input: queries = ["word", "note", "ants", "wood"] dictionary = ["world", "leetcode", "note", "wood"]
โ€บ Output: ["word", "note", "wood"]
๐Ÿ’ก Note: "word" differs from "world" by 1 character (dโ†’l), "note" matches exactly (0 edits), and "wood" matches exactly. "ants" would need more than 2 edits to match any dictionary word.
example_2.py โ€” All Match
$ Input: queries = ["yes"] dictionary = ["not"]
โ€บ Output: []
๐Ÿ’ก Note: "yes" differs from "not" in all 3 positions (yโ‰ n, eโ‰ o, sโ‰ t), requiring 3 edits which exceeds the limit of 2.
example_3.py โ€” Edge Case
$ Input: queries = ["abc", "def"] dictionary = ["abc", "ghi", "aef"]
โ€บ Output: ["abc", "def"]
๐Ÿ’ก Note: "abc" matches exactly with dictionary word "abc" (0 edits). "def" can become "aef" with 1 edit (dโ†’a).

Visualization

Tap to expand
Word Transformation Within 2 EditsQuery: "word"Target to transformDictionary: "world"Comparison targetResult: Match!1 edit โ‰ค 2 editsCharacter-by-Character Comparison:wordwordworlworldโœ“ Sameโœ“ Sameโœ“ Sameโœ— Different (dโ†’l)Edit Count AnalysisPositions 0,1,2: Match (0 edits)Position 3: Different (1 edit)Total: 1 edit โ‰ค 2 edits โœ“1Step 1: Count differences2Step 2: Check threshold (โ‰ค 2)3Step 3: Add to results
Understanding the Visualization
1
Take Query Word
Start with a word from the queries array
2
Compare Characters
Go through each position and count differences with dictionary words
3
Early Termination
Stop counting if differences exceed 2 (optimization)
4
Decision
Add to result if any dictionary word is within 2 edits
Key Takeaway
๐ŸŽฏ Key Insight: Since all words have the same length, we only need to count character differences at corresponding positions. No complex dynamic programming needed - just simple character comparison with early termination when differences exceed 2!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— m ร— k)

n queries ร— m dictionary words ร— k character comparisons, but with better constants

n
2n
โœ“ Linear Growth
Space Complexity
O(m)

Space for dictionary hash set containing m words

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค queries.length, dictionary.length โ‰ค 1000
  • queries[i].length = dictionary[j].length for all valid i, j
  • 1 โ‰ค queries[i].length โ‰ค 100
  • All queries[i] and dictionary[j] consist of lowercase English letters only
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
26.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen