letter-combination-phone-number.sh — zsh

Letter Combinations of a Phone Number

medium
stringsbacktracking

Given a string of digits from 2 through 9, return every possible letter combination using the traditional telephone keypad mapping. Preserve digit order and emit combinations in keypad order. Return an empty list for an empty input.

Input / output

  • Input: digits: string
  • Output: string[]

Examples

  1. "23" returns ["ad","ae","af","bd","be","bf","cd","ce","cf"].
  2. "" returns [].

Constraints

  • 0 <= digits.length <= 4
  • Every character is between 2 and 9.

Follow-up What are the time and output-space bounds when digits 7 and 9 contribute four choices?

Examples
Example 1
Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
Example 2
Input: digits = ""
Output: []
Example 3
Input: digits = "2"
Output: ["a","b","c"]
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.