본문 바로가기
Problem Solving/리트코드

[리트코드 leetcode] 17. Letter Combinations of a Phone Number

by ggyongi 2021. 4. 12.
반응형

leetcode.com/problems/letter-combinations-of-a-phone-number/

 

Letter Combinations of a Phone Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        dict ={
            '2':'abc',
            '3':'def',
            '4':'ghi',
            '5':'jkl',
            '6':'mno',
            '7':'pqrs',
            '8':'tuv',
            '9':'wxyz'
        }
        
        
        if digits =="":
            return []
        
        digits_list = []
        for i in range(len(digits)):
            digits_list.append(digits[i])
        
        output =[]
        
        def combineNext(letter='',i=0):
            if len(digits_list)>i:
                digit= digits_list[i]
                for le in dict[digit]:
                    k = letter + le
                    combineNext(k,i+1)
                    
            else:
                output.append(letter)
        
        combineNext()
        return output
          
            
            
class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        if not digits:
            return ''
        
        table={
            '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'],
        }
        
        result=[]   
    
        def dfs(word, idx):  
            if idx < len(digits):
                char_list = table[digits[idx]]
                for char in char_list:
                    dfs(word+char, idx+1)        
            else:
                result.append(word)
        
        dfs('',0)
        return result
class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        if not digits:
            return ''
        
        table={
            '2':'abc',
            '3':'def',
            '4':'ghi',
            '5':'jkl',
            '6':'mno',
            '7':'pqrs',
            '8':'tuv',
            '9':'wxyz',
            
        }
        result=[]   
    
        def dfs(word, idx):  
            if idx == len(digits):
                result.append(word)
                return
            
            for char in table[digits[idx]]:
                dfs(word+char, idx+1)
        
        dfs('',0)
        return result
 

비전공자 네카라 신입 취업 노하우

시행착오 끝에 얻어낸 취업 노하우가 모두 담긴 전자책!

kmong.com

댓글