Letter Combinations of a Phone Number - Problem
Letter Combinations of a Phone Number

Remember the old phone keypads where you had to press keys multiple times to type letters? This problem takes us back to that era!

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. The mapping follows the classic telephone keypad:

2: ABC, 3: DEF, 4: GHI, 5: JKL, 6: MNO, 7: PQRS, 8: TUV, 9: WXYZ

For example, if someone gives you "23", you need to find all ways to combine letters from key 2 (A, B, C) with letters from key 3 (D, E, F). The result would be: ["AD", "AE", "AF", "BD", "BE", "BF", "CD", "CE", "CF"]

Goal: Generate all possible text combinations that could be typed using the given sequence of key presses.
Input: A string of digits (2-9)
Output: Array of all possible letter combinations

Input & Output

example_1.py β€” Basic Case
$ Input: digits = "23"
β€Ί Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
πŸ’‘ Note: Digit '2' maps to 'abc' and digit '3' maps to 'def'. We form all combinations by pairing each letter from '2' with each letter from '3': a with d/e/f, b with d/e/f, c with d/e/f.
example_2.py β€” Empty Input
$ Input: digits = ""
β€Ί Output: []
πŸ’‘ Note: When the input string is empty, there are no digits to process, so we return an empty array.
example_3.py β€” Single Digit
$ Input: digits = "2"
β€Ί Output: ["a","b","c"]
πŸ’‘ Note: For a single digit '2', we simply return all the letters it maps to: 'a', 'b', and 'c'.

Constraints

  • 0 ≀ digits.length ≀ 4
  • digits[i] is a digit in the range ['2', '9']
  • The maximum number of combinations is 44 = 256 (when all digits are 7 or 9)

Visualization

Tap to expand
Phone Keypad Combination GeneratorPhone Keypad2ABC3DEF4GHI5JKL6MNO7PQRS8TUV9WXYZDecision Tree for "23"startABCDEFDEFDEFADAEAFBDBEBFCDCECFHow Backtracking Works:1. Choose: Pick a letter for current digit (e.g., 'A' for digit '2')2. Explore: Recursively solve for remaining digits ('3' β†’ D, E, F)3. Collect: When no more digits, add combination to results4. Backtrack: Return to previous choice and try next option5. Repeat: Continue until all paths in the decision tree are explored
Understanding the Visualization
1
Map digits to letters
Create the phone keypad mapping: 2β†’ABC, 3β†’DEF, etc.
2
Start recursive exploration
Begin at the root with empty combination, process first digit
3
Branch for each choice
For each possible letter, create a branch and recurse to next digit
4
Collect complete paths
When all digits are processed, add the combination to results
5
Backtrack and continue
Return to previous level and try next option until all paths explored
Key Takeaway
🎯 Key Insight: Use backtracking to systematically explore all possible paths in the decision tree, building combinations one character at a time and collecting results when complete paths are found.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
89.3K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen