aboutsummaryrefslogtreecommitdiffstats
path: root/91/main.py
blob: 3099c7be04a1acf97199d8995f7ff5b51a301e5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python


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

        dp = [0] * (len(s) + 1)
        dp[0], dp[1] = 1, 1

        for i in range(2, len(s) + 1):
            print(s[i - 1 : i])
            if 0 < int(s[i - 1 : i]) <= 9:
                dp[i] += dp[i - 1]
            print(s[i - 2 : i])
            if 10 <= int(s[i - 2 : i]) <= 26:
                dp[i] += dp[i - 2]

        return dp[-1]


def main():
    solution = Solution()
    print(solution.numDecodings("12"))
    print(solution.numDecodings("226"))
    print(solution.numDecodings("06"))


if __name__ == "__main__":
    main()