Skip to content

Commit 03f3f1a

Browse files
committed
decode ways solution
1 parent 5c4d5fb commit 03f3f1a

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

decode-ways/yuseok89.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# TC: O(N)
2+
# SC: O(N)
3+
class Solution:
4+
5+
memo = []
6+
7+
def rec(self, idx, s, n):
8+
if self.memo[idx] == -1:
9+
10+
cnt = 0
11+
12+
if int(s[idx]) != 0:
13+
cnt += self.rec(idx + 1, s, n)
14+
15+
if idx + 1 < n:
16+
if int(s[idx]) != 0 and int(s[idx]) * 10 + int(s[idx + 1]) <= 26:
17+
cnt += self.rec(idx + 2, s, n)
18+
19+
self.memo[idx] = cnt
20+
21+
return self.memo[idx]
22+
23+
def numDecodings(self, s: str) -> int:
24+
25+
self.memo = [-1] * (len(s) + 1)
26+
self.memo[len(s)] = 1
27+
28+
return self.rec(0, s, len(s))
29+

0 commit comments

Comments
 (0)