-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdecode_ways.py
More file actions
32 lines (31 loc) · 1.05 KB
/
decode_ways.py
File metadata and controls
32 lines (31 loc) · 1.05 KB
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
32
class Solution: # 44ms, rule based DP, Credits - LeetCode, iterative
# @param s, a string
# @return an integer
def numDecodings(self, s):
#dp[i] = dp[i-1] if s[i] != "0"
# +dp[i-2] if "09" < s[i-1:i+1] < "27"
if s == "": return 0
dp = [0 for x in range(len(s)+1)]
dp[0] = 1
for i in range(1, len(s)+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": #"01"ways = 0
dp[i] += dp[i-2]
return dp[len(s)]
class Solution: # Credits- LeetCode, recursive, O(n), O(n)
def numDecodings(self, s: 'str') -> 'int':
def helper(i):
if i in dic:
return dic[i]
if i >= len(s):
return 1
res = 0
if 1 <= int(s[i]) <= 9:
res += helper(i+1)
if 10 <= int(s[i:i+2]) <= 26:
res += helper(i+2)
dic[i] = res
return res
dic = {}
return helper(0) if s else 0