Scribbling

LeetCode: 91. Decode Ways 본문

Computer Science/Coding Test

LeetCode: 91. Decode Ways

focalpoint 2021. 10. 11. 15:32
class Solution:
    def numDecodings(self, s: str) -> int:
        candidates = set([str(i) for i in range(1, 27)])
        # dp[i] denotes # of ways until s[i-1]
        dp = [0] * (len(s) + 1)
        dp[0] = 1
        if s[0] in candidates:
            dp[1] = 1
        
        for i in range(2, len(s)+1):
            # dp[n] = dp[n-2] * k2 + dp[n-1] * k1
            k2 = 0
            if s[i-2:i] in candidates:
                k2 += 1
            k1 = 0
            if s[i-1] in candidates:
                k1 += 1
            dp[i] = dp[i-2] * k2 + dp[i-1] * k1
        return dp[len(s)]

'Computer Science > Coding Test' 카테고리의 다른 글

LeetCode: 97. Interleaving String  (0) 2021.10.11
LeetCode: 93. Restore IP Addresses  (0) 2021.10.11
LeetCode: 100. Same Tree  (0) 2021.10.11
LeetCode: 90. Subsets II  (0) 2021.10.10
LeetCode: 88. Merge Sorted Array  (0) 2021.10.09