Letter Combinations of a Phone Number
List every string a phone keypad can type from digits
DSA, Medium, Backtracking
Which structure or algorithm, and what does it cost.
On an old phone keypad each digit from 2 to 9 stands for letters: 2 abc, 3 def, 4 ghi, 5 jkl, 6 mno, 7 pqrs, 8 tuv, 9 wxyz. A string digits holds only the digits 2 to 9. Return every string you can type by choosing one letter for each digit, in order. List the strings in alphabetical order.
Examples
Constraints
- 1 <= length of
digits<= 6. - Each character is a digit from 2 to 9.
Your answer must contain
- The method; why every string appears once and in order; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n * 4^n) time, because the answer has up to 4^n strings of n letters.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Take the digits one at a time. How many letters can the first digit give you?
Hint 2 the approach
Choosing a letter for the first digit leaves the same question for the other digits. When no digits are left, you have a string.
Hint 3 the key step
Keep the letters chosen so far. For the current digit, loop over its letters in order, add one, recurse on the next digit, then remove it.
Walkthrough
Full walkthrough brute force to best
Brute force
Take every string of n letters from the whole alphabet, which is 26^n strings, and keep those where letter k belongs to digit k. For n = 6 that is 308,915,776 strings to test, while at most 4^6 = 4,096 of them are answers.
Best method
Walk through the digits from left to right. Keep the letters chosen so far in a list. At digit k, loop over the letters of that digit in order. Add one, call again for digit k + 1, then remove it. When k equals the number of digits, join the list into a string and record it. The letters of each digit are in order and the first digit changes slowest, so the strings come out in alphabetical order with no sorting. Every branch ends in an answer, so no work is wasted.
Hand trace
Run on 47. Digit 4 has g, h, i. Add g. Digit 7 has p, q, r, s. Add p and record gp. Remove p, add q and record gq. Then gr, then gs. Remove g and add h. The same four letters give hp, hq, hr and hs. Remove h and add i. That gives ip, iq, ir and is. Twelve strings in alphabetical order.
Code
def letter_combinations(digits):
letters = {"2": "abc", "3": "def", "4": "ghi", "5": "jkl",
"6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"}
out = []
path = []
def go(k):
if k == len(digits):
out.append("".join(path))
return
for ch in letters[digits[k]]:
path.append(ch)
go(k + 1)
path.pop()
go(0)
return outTime and memory
O(n * 4^n) time: at most 4^n strings, because digits 7 and 9 have four letters, and each string takes n steps to build. Extra memory is O(n) for the list and the recursion, plus the answer.
Common mistakes
- Looping over all the digits inside each call. One call must handle one digit, and the recursion handles the rest.
- Forgetting to remove the letter after the call. The next letter is then added after the old one and the strings grow too long.
- Giving every digit three letters. Digits 7 and 9 have four.