MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / Solution

Class Solution

DP/DecodeWays.py:54–98  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

52
53"""
54class Solution(object):
55 def numDecodings(self, s):
56 """
57 :type s: str
58 :rtype: int
59 """
60 if not s:
61 return 0
62 if len(s) == 1:
63 if 0 < int(s) < 10:
64
65 return 1
66 return 0
67
68 letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
69 letters_dict = {str(i):letters[i-1] for i in range(1, 27)}
70 dp = []
71 if 0 < int(s[0]) < 10:
72 dp.append(1)
73 else:
74 dp.append(0)
75
76 if 10 <= int(s[0]+s[1]) < 27:
77 if 0 < int(s[1]) < 10:
78 dp.append(2)
79 else:
80 dp.append(1)
81 else:
82 if s[1] != '0':
83 dp.append(dp[0])
84 else:
85 dp.append(0)
86
87 for i in range(2, len(s)):
88 x = 0
89 if s[i] != '0':
90 x += dp[i-1]
91
92 if s[i-1] != '0':
93 if '10' <= s[i-1] + s[i] < '27':
94
95 x += dp[i-2]
96 dp.append(x)
97
98 return dp[-1]
99# self.result = 0
100
101# def makeDecode(rest_str):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected