17 - Letter Combinations of a Phone Number
📌 문제
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 digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
📋 코드
from itertools import product
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
number = [[], [], ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'],
['j', 'k', 'l'], ['m', 'n', 'o'], ['p', 'q', 'r', 's'],
['t', 'u', 'v'], ['w', 'x', 'y', 'z']]
if not digits:
return []
digit = list(map(int, digits))
select = []
for d in digit:
select.append(number[d])
prod = list(product(*select))
result = []
for char in prod:
tmp = ""
for c in char:
tmp += c
result.append(tmp)
return result
💡 한마디
두 개 이상 리스트의 조합을 구하고자 할 때에는 product
를 이용하자 !