Loading [MathJax]/jax/output/CommonHTML/jax.js
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜/LEETCODE

Letter Combinations of a Phone Number | LeetCode 793 | Python3 ๐Ÿ

๋ฐ˜์‘ํ˜•

 

 

๐Ÿค” ๋ฌธ์ œ : Letter Combinations of a Phone Number | LeetCode 793 

๋ฌธ์ œ: https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/793/
์ˆซ์ž -> 3~4๊ฐœ์˜ ๋ฌธ์ž ๋กœ ๋งคํ•‘๋  ๋•Œ

์ˆซ์ž input์ด ์ฃผ์–ด์ง€๋ฉด ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ฌธ์ž output์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. 

 

๐Ÿ’ก ํ’€์ด

1. ์ ‘๊ทผ -  BFS? DFS?

BFS : ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์€ ๋งน๋ชฉ์  ํƒ์ƒ‰๋ฐฉ๋ฒ•์˜ ํ•˜๋‚˜๋กœ ์‹œ์ž‘ ์ •์ ์„ ๋ฐฉ๋ฌธํ•œ ํ›„ ์‹œ์ž‘ ์ •์ ์— ์ธ์ ‘ํ•œ ๋ชจ๋“  ์ •์ ๋“ค์„ ์šฐ์„  ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ๋ฒ•

DFS : ํ•˜๋‚˜์˜ ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•œ ๋’ค์—์•ผ ๋‹ค๋ฅธ ์ด์›ƒ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ๋ฒ•

 

input์ด 23์ด๋ผ๊ณ  ํ•  ๋•Œ BFS, DFS๋Š” ๊ฐ๊ฐ ์•„๋ž˜์˜ ์ˆœ์„œ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค.

 

BFS

  1. ์ฒซ๋ฒˆ์งธ ์ž๋ฆฌ์— ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž ํƒ์ƒ‰ => a b c
  2. ๋‘๋ฒˆ์งธ ์ž๋ฆฌ์— ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž ํƒ์ƒ‰ => ad ae af bd be bf cd ce cf

 

DFS

  1. a๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ ํƒ์ƒ‰ => ad ae af
  2. b๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ ํƒ์ƒ‰ => ad ae af bd be bf 
  3. c๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ ํƒ์ƒ‰ => ad ae af bd be bf cd ce cf

 

 

2.  ํ’€์ด1- recursion์„ ํ™œ์šฉํ•œ DFS

 class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        phone_pad = {"0" : [" "], "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 = []
        digit_length = len(digits)
        if digit_length is 0: return []
    
        def makeString(given_string, index):
            if index is digit_length-1:
                for c in phone_pad[digits[index]]:
                    result.append(given_string+c)
            else:
                for c in phone_pad[digits[index]]:
                    makeString(given_string+c, index+1)
    
        makeString('', 0)
        return result

 

runtime์€ 63ms๋กœ ํ•˜์œ„ 8%์— ์†ํ•˜๋„ค์š”.

recursiveํ•˜๊ฒŒ call stack์„ ๋งŒ๋“ค์–ด๋‚˜๊ฐ€๋Š” ์—ฐ์‚ฐ ์ž์ฒด๊ฐ€ expensiveํ•˜๊ธฐ ๋•Œ๋ฌธ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

 

2.  ํ’€์ด2- DFS

class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        phone_pad = {"0" : [" "], "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 = phone_pad[digits[0]] if digits else []
        for d in digits[1:]:
            new_result = []
            for c in phone_pad[d]:
                for str in result:
                    new_result.append(str+c)
            result = new_result
        return result

3์ค‘ ํฌ๋ฌธ์„ ํ™œ์šฉํ•œ DFS๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. 

ํƒ์ƒ‰ ํšŸ์ˆ˜๋Š” ๋™์ผํ•  ๊ฒƒ ๊ฐ™์€๋ฐ call stack์ด ์—†์–ด์„œ์ธ์ง€ ์†๋„๊ฐ€ ํ›จ์”ฌ ๋นจ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. 

 

 

๋ฐ˜์‘ํ˜•