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:
For example, if someone gives you
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
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: WXYZFor 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
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.
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code