Back to Trie
Course Practice
Medium

Words Within Two Edits of Dictionary

LAB

Return query words that differ from at least one dictionary word in at most two positions, preserving query order.

EXAMPLES

Example 1
Input
{
  "queries": [
    "word",
    "note",
    "ants",
    "wood"
  ],
  "dictionary": [
    "wood",
    "joke",
    "moat"
  ]
}

Output
[
  "word",
  "note",
  "wood"
]

FUNCTION SHAPE

queries: stringArraydictionary: stringArraystringArray
SOLUTION NOTE

This is a simple trie application. We try all children in the trie, and when there is a mismatch while traversing the trie, we reduce the edit count by one. When the edit count is negative we return False. If we reach the end of the word with at most 2 edits, this word works. If any such children work, we return True.

Open on LeetCode
00:00
2 local tests readyRun with ⌘/Ctrl + Enter. Your code stays in this browser.

Runs solve(...) locally in a browser worker. SWE Playbook does not submit your code. Only run code you trust; Python code may access the network.