Letter Combinations of a Phone Number - Problem

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Digit Mapping:

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

Input & Output

Example 1 — Two Digits
$ Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
💡 Note: Digit 2 maps to 'abc', digit 3 maps to 'def'. Each letter from 2 combined with each letter from 3 gives 3×3=9 combinations.
Example 2 — Empty Input
$ Input: digits = ""
Output: []
💡 Note: Empty input string returns empty array as no digits to process.
Example 3 — Single Digit
$ Input: digits = "2"
Output: ["a","b","c"]
💡 Note: Single digit 2 maps to letters 'a', 'b', 'c', so return each letter as separate combination.

Constraints

  • 0 ≤ digits.length ≤ 4
  • digits[i] is a digit in the range ['2', '9']

Visualization

Tap to expand
Letter Combinations: Phone Keypad MappingInput:"23"Digit Mapping2 → ABC3 → DEFAll CombinationsA×D, A×E, A×FB×D, B×E, B×F, C×D, C×E, C×FProcess: Generate Cartesian ProductFor each letter in digit 1, combine with each letter in digit 23 letters × 3 letters = 9 total combinationsOutput: ["ad","ae","af","bd","be","bf","cd","ce","cf"]Array of 9 string combinations
Understanding the Visualization
1
Input
String of digits 2-9, each mapping to letters
2
Mapping
Each digit maps to 3-4 letters like phone keypad
3
Combinations
Generate all possible letter combinations
4
Output
Array of all valid combinations
Key Takeaway
🎯 Key Insight: Each digit choice creates multiple branches - systematically explore all combinations using backtracking or iterative building
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 28
68.0K 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