티스토리 뷰

공부

[leetcode] 91. Decode Ways

승가비 2022. 12. 12. 01:30
728x90

https://leetcode.com/problems/decode-ways/description/

 

Decode Ways - 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

https://www.techiedelight.com/ko/count-decodings-sequence-of-digits/

 

주어진 숫자 시퀀스의 카운트 디코딩

양수가 주어지면 해당 숫자를 매핑 테이블의 해당 알파벳에 매핑합니다. [(1, 'A'), (2, 'B'), … (26, 'Z')], 가능한 총 디코딩 횟수를 반환합니다. 입력 번호가 매핑 테이블에 있는 유효한 한 자리 또는

www.techiedelight.com

class Solution:
    def numDecodings(self, s: str) -> int:
        if s == "": 
            return 0

        size = len(s)
        
        dp = [0 for x in range(size + 1)]
        dp[0] = 1

        for i in range(1, size + 1):
            if s[i - 1] != "0":
                dp[i] += dp[i - 1]

            if i != 1 and s[i - 2:i] < "27" and s[i - 2:i] > "09":
                dp[i] += dp[i - 2]

        return dp[len(s)]
728x90
댓글