FastPrepKnown Words Matching Phone Digits

Known Words Matching Phone Digits

LinkedIn logoLinkedIn● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given a dictionary of known lowercase words and a string of telephone digits. Return the known words that exactly match the digit string using this mapping:

  • 2: abc
  • 3: def
  • 4: ghi
  • 5: jkl
  • 6: mno
  • 7: pqrs
  • 8: tuv
  • 9: wxyz

A matching word has exactly one letter for every digit, and each letter maps to the digit at the same position. Return each matching word once, in ascending lexicographic order. Return an empty array when there is no match.

Thread-safety discussion

Discuss how several callers could query a shared dictionary or lookup cache safely. Explain how to publish a complete dictionary snapshot, protect mutable shared state and avoid returning a cached answer from the wrong dictionary version. The judged callable remains a finite sequential lookup and does not require implementing threads.

Function

matchingWords(words: String[], digits: String) → String[]

Examples

Example 1

words = ["tree","used","apple"]digits = "8733"return = ["tree","used"]

Both tree and used map to 8733. apple has a different length and signature.

Example 2

words = ["a","b","a","d"]digits = "2"return = ["a","b"]

The repeated dictionary entry a appears only once in the result.

Constraints

  • 0 <= words.length <= 2000.
  • Every word contains 1 to 20 lowercase English letters.
  • 1 <= digits.length <= 20; every digit is from 2 through 9.
  • Dictionary entries may repeat.

More LinkedIn problems

See LinkedIn hiring insights
public String[] matchingWords(String[] words, String digits) {
    // Write your code here.
}
words["tree","used","apple"]
digits"8733"
expected["tree", "used"]
Checking account…