Meta (Facebook) · Question 37 · Backtracking
Letter Combinations of Phone Number
Problem
You are given a string containing digits 2-9. Return all possible letter combinations from phone keypad.
Approach
DFS over digits, expanding each into its keypad letters.
Time O(4^n)Space O(n)
How it works
- 1Map each digit to its letters.
- 2DFS appends one letter per digit position.
Sample input / output
Input: 23 Output: ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']
Constraints
0 <= digits.length <= 4; digits 2-9 only.
Solution — read it, then walk it
Time O(4^n)Space O(n)Loops 1Branches 3Returns 3source · Python
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/22
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits