17. Letter Combinations of a Phone Number
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return []
d = {
'2': ['a', 'b', 'c'],
'3': ['d', 'e', 'f'],
'4': ['g', 'h', 'i'],
'5': ['j', 'k', 'l'],
'6': ['m', 'n', 'o'],
'7': ['p', 'q', 'r', 's'],
'8': ['t', 'u', 'v'],
'9': ['w', 'x', 'y', 'z']
}
ans = [""]
for digit in digits:
next_level = []
for char in d[digit]:
for partial in ans:
next_level.append(partial+char)
ans = next_level[:]
return ans- for each number in
digits, the number of possibilities grows exponentially.- so, runtime is approximately where is the length of
digits.
- so, runtime is approximately where is the length of
- we work through each digit, and for each new digit, we append each possible character for that
digitto each existing partial solution.
Categories:: hashmap, string, backtracking