#2452
Words Within Two Edits of Dictionary
specialist · 690 · lc medium +30 · premium · verified · 61.8% accepted · 325 likes · top 62%
Description
You are given two string arrays queries and dictionary. All strings in both arrays have the same length.
An edit operation replaces one character in a word. For each query, determine whether it can be converted to any string in dictionary using at most two edits.
Return an array of all queries that satisfy this condition, in their original order.
Example 1:
Input: queries = ["word","note","ants","wood"], dictionary = ["wood","joke","moat"]
Output: ["word","note","wood"]
Explanation:
- Changing the 'r' in "word" to 'o' allows it to equal the dictionary word "wood".
- Changing the 'n' to 'j' and the 't' to 'k' in "note" changes it to "joke".
- It would take more than 2 edits for "ants" to equal a dictionary word.
- "wood" can remain unchanged (0 edits) and match the corresponding dictionary word.
Thus, we return ["word","note","wood"].
Example 2:
Input: queries = ["yes"], dictionary = ["not"]
Output: []
Explanation:
Applying any two edits to "yes" cannot make it equal to "not". Thus, we return an empty array.
Code
1
2
3