Keypad Digit Sequence Word Filter
StringsAuto-graded · 24 test casesLast updated
Filter a word list down to the words whose letters, read through a phone keypad, spell out a given digit string, in original order.
Keypad Digit Sequence Word Filter
An old mobile phone keypad assigns letters to the digit keys as follows:
| Key | Letters |
|---|---|
| 2 | a, b, c |
| 3 | d, e, f |
| 4 | g, h, i |
| 5 | j, k, l |
| 6 | m, n, o |
| 7 | p, q, r, s |
| 8 | t, u, v |
| 9 | w, x, y, z |
Typing a word means pressing, for each of its letters, the key that holds that letter. This produces a digit string for the word.
You are given a string digits and a list of words dictionary. Return every word from dictionary whose typed digit string is exactly digits.
Matching rules
- A word matches only if its length equals
len(digits)and, at every position, the key holding the word's letter is the digit at that same position indigits. - Return matching words in the same order they appear in
dictionary. If a word appears several times indictionary, keep every copy. - If
digitsis empty, return an empty list. - The keys
0and1(and any character that is not one of2-9) hold no letters, so ifdigitscontains such a character, no word can match and the result is an empty list. - A word containing a character that is not on the keypad (for example an uppercase letter) never matches.
- If
dictionaryis empty or nothing matches, return an empty list.
Constraints
0 <= len(digits) <= 100 <= len(dictionary) <= 100,000- Each word consists of lowercase English letters.
Examples
Example 1
Input: find_words_for_digits('23', ['ad', 'ae', 'af', 'bd', 'cat'])
Output: ['ad', 'ae', 'af', 'bd']
Example 2
Input: find_words_for_digits('4663', ['good', 'home', 'gone', 'food'])
Output: ['good', 'home', 'gone']
Hints
Hint 1
Rather than generating every letter combination from the digits (which can reach about a million strings), convert each dictionary word into digits and compare.
Hint 2
Build a letter-to-digit lookup once. Then a word can be rejected immediately if its length differs from the length of digits, and also at the first position where its letter's key differs from the digit.
Hint 3
Handle the special cases up front: an empty digit string, or one containing a character outside 2-9, can never match anything, so return an empty list straight away.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
There are two ways to relate digits and words:
- Expand
digitsinto every possible letter string and look each one up in the dictionary. With up to 10 digits this can produce around 4^10, roughly a million, candidates, most of them meaningless. - Encode each dictionary word into digits and compare it with
digits. Each word costs at mostlen(digits)checks.
The second option is much cheaper, so we use it.
First, build a reverse map from each letter to its key once. Then:
- If
digitsis empty or contains a character outside2-9, return[]immediately, since such input cannot match any word. - For each word in order, skip it if its length differs from
len(digits). - Otherwise check, position by position, that the letter's key equals the digit. A letter missing from the map yields
None, which never equals a digit, so the word is rejected. - Append words that pass every check.
Why it is correct
Every keypad letter lives on exactly one key, so a word's typed digit string is well defined. A word produces digits exactly when it has the same length and each letter maps to the corresponding digit. The loop tests exactly these two conditions for every word, so it neither misses valid words nor accepts invalid ones. Iterating the dictionary in order and appending preserves order and duplicates.
Complexity
Let N be the number of words and L = len(digits) (at most 10).
- Time: O(N * L) in the worst case. The length check is O(1), and only words of length
Lare scanned, each for at mostLcharacters. - Space: O(1) auxiliary (the letter map has 26 entries), plus the output list.
Reference solution
from typing import List
def find_words_for_digits(digits: str, dictionary: List[str]) -> List[str]:
if not digits or any(d not in "23456789" for d in digits):
return []
keypad = {
"2": "abc", "3": "def", "4": "ghi", "5": "jkl",
"6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz",
}
letter_to_digit = {
letter: d
for d, letters in keypad.items()
for letter in letters
}
n = len(digits)
result = []
for word in dictionary:
if len(word) != n:
continue
if all(letter_to_digit.get(ch) == d for ch, d in zip(word, digits)):
result.append(word)
return result