Ctrl K

Nearly Dictionary Words

Medium
Players type in code words, and the game forgives small typos. You get a list of typed words words and a dictionary vocab. Every word in both lists is made of lowercase English letters, and all of them have the same length. One edit replaces a single letter of a word with any other letter. A word from words is accepted if at most two edits turn it into some word of vocab (zero edits count too). Return all accepted words in the order they appear in words.
Example 1
words=["cart", "milk", "bred", "jamm"], vocab=["card", "brew", "silk"] → ['cart', 'milk', 'bred']

cart → card and bred → brew take one change each, and so does milk → silk. But jamm differs from every vocabulary word in at least three positions.

Example 2
words=["abc"], vocab=["xyz"] → []

All three letters differ, so two changes are not enough.

Example 3
words=["moon", "moon"], vocab=["noon"] → ['moon', 'moon']

Repeated words stay in the answer: each occurrence is checked on its own.

Constraints
  • 1 ≤ len(words), len(vocab) ≤ 100
  • All words have the same length n, 1 ≤ n ≤ 100
  • Words consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSplit Into Dictionary Words MediumHighlight Search Hits MediumShorten Words to Their Roots MediumMost Searched Words MediumWord Grown Letter by Letter Medium
loading Python…
Input
words=["cart", "milk", "bred", "jamm"], vocab=["card", "brew", "silk"]
Expected
['cart', 'milk', 'bred']

Run checks the first 3 tests in your browser. Submit checks all 76 tests on the server, hidden ones included.