본문 바로가기

Leetcode

Leetcode 17. Letter Combinations of a Phone Number

오랜만에 리트코드를 켰다. 이제 서서히 공부를 시작해나갔으면 좋겠다.

사실 연구 하기 싫어서 리트코드 한건 안비밀...

 


class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        dictionary = {'2':'abc', '3':'def', '4':'ghi', '5':'jkl', '6':'mno', '7':'pqrs', '8':'tuv', '9':'wxyz'}
        if not digits:
            return []
        output = ['']
        for idx in digits:
            output = [prev + l for prev in output for l in dictionary[idx]]
        return output