Find dictionary words matching a phone digit string
Company: LinkedIn
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates familiarity with phone-keypad character-to-digit mappings, string pattern matching, and efficient filtering of dictionary words, testing competencies in mapping, string manipulation, and algorithmic performance within the coding & algorithms domain.
Constraints
- 1 <= len(digits) <= 20
- 0 <= len(words) <= 2 * 10^5
- Each word contains only lowercase English letters
- Each word has length at most 20
Examples
Input: ("8733", ["tree", "used", "free", "true"])
Expected Output: ["tree", "used"]
Explanation: `tree` maps to 8733 and `used` also maps to 8733. `free` and `true` do not match the pattern.
Input: ("23", ["ad", "ae", "bd", "cf", "dog", "a"])
Expected Output: ["ad", "ae", "bd", "cf"]
Explanation: The first four words match 2->abc and 3->def. `dog` and `a` are rejected because their lengths do not match.
Input: ("2", [])
Expected Output: []
Explanation: An empty dictionary produces no matches.
Input: ("4663", ["good", "home", "gone", "hood", "foo", "good"])
Expected Output: ["good", "home", "gone", "hood", "good"]
Explanation: All listed 4-letter words except `foo` map to 4663. The duplicate `good` should be preserved.
Input: ("23", ["aa", "gg", "zzz"])
Expected Output: []
Explanation: `aa` maps to 22, `gg` maps to 44, and `zzz` has the wrong length, so nothing matches.
Hints
- Instead of checking whether each digit contains a letter, build a reverse mapping from each letter to its corresponding digit.
- Skip any word whose length does not match `len(digits)` before checking character-by-character.