LeetCode 17

HJ seo·2022년 10월 23일
0

Coding Test(Python)

목록 보기
40/45

문제 링크

문제 설명.


위의 사진과 같은 번호와 문자들이 적힌 전화가 있다.

2부터 9사이의 숫자들이 입력된 임의의 digit : string이 주어졌을 때 다음 번호와 매칭되는 모든 영문자열을 구하라.

example.

input : digit == "34"
output : ['dg','eg','fg','dh','eh','fh','di','ei','fi']

풀이코드.

class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        if not digits:
            return []
        
        leng = len(digits)
        changer = {'2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz'}
        curr = ['']
        nex = []
        l = 0
        while l != leng:
            for i in curr:
                for j in changer[digits[l]]:
                    nex.append(i+j)
            
            curr = nex
            nex = []
            l += 1
        
        return curr

result.

Runtime: 32 ms, faster than 93.47% of Python3 online submissions for Letter Combinations of a Phone Number.
Memory Usage: 13.9 MB, less than 79.61% of Python3 online submissions for Letter Combinations of a Phone Number.

잡담.

코루틴을 공부하다가 스터디로 인해 급하게 글을 쓰게 되었다. 보통 daily 문제를 주로 올리는 편이지만 문제가 어렵지 않아서 pass..

profile
다양한 분야에 관심이 많은 초보 개발자 입니다.

0개의 댓글