Skip to content

Commit 4bff29b

Browse files
committed
decode-ways solution
1 parent 227601a commit 4bff29b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

decode-ways/sun912.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
TC: O(n)
3+
SC: O(n)
4+
5+
DNF, read solutions by Dale...ㅠㅠ
6+
"""
7+
8+
class Solution:
9+
def numDecodings(self, s: str) -> int:
10+
memo = {len(s): 1}
11+
12+
def dfs(start):
13+
if start in memo:
14+
return memo[start]
15+
16+
if s[start] == "0":
17+
return 0
18+
if start + 1 < len(s) and int(s[start: start+2]) < 27:
19+
memo[start] = dfs(start + 1) + dfs(start + 2)
20+
else:
21+
memo[start] = dfs(start + 1)
22+
return memo[start]
23+
24+
return dfs(0)

0 commit comments

Comments
 (0)