-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0091-decode-ways.py
More file actions
48 lines (41 loc) · 1.15 KB
/
0091-decode-ways.py
File metadata and controls
48 lines (41 loc) · 1.15 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# time complexity: O(n)
# space complexity: O(n)
from functools import lru_cache
# Top Down
class Solution:
def numDecodings(self, s: str) -> int:
@lru_cache(None)
def dp(idx: int) -> int:
if idx == len(s):
return 1
if s[idx] == '0':
return 0
if idx == len(s) - 1:
return 1
result = dp(idx + 1)
if int(s[idx: idx + 2]) <= 26:
result += dp(idx + 2)
return result
return dp(0)
# time complexity: O(n)
# space complexity: O(n)
class Solution:
def numDecodings(self, s: str) -> int:
if s[0] == '0':
return 0
n = len(s)
dp = [0 for _ in range(n + 1)]
dp[0] = 1
dp[1] = 1
for i in range(2, n + 1):
if s[i-1] != '0':
dp[i] += dp[i - 1]
if s[i-2] == '1' or (s[i - 2] == '2' and s[i - 1] <= '6'):
dp[i] += dp[i - 2]
return dp[n]
s = "12"
print(Solution().numDecodings(s))
s = "226"
print(Solution().numDecodings(s))
s = "06"
print(Solution().numDecodings(s))