Skip to content

Commit 0202307

Browse files
authored
Create Decode-Ways.py
1 parent 411805e commit 0202307

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Leetcode/Decode-Ways.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
4+
def helper(s, index, cache):
5+
if index == 0:
6+
return 1
7+
starting_index = len(s) - index
8+
if s[starting_index] == "0":
9+
return 0
10+
if index in cache:
11+
return cache[index]
12+
13+
result = helper(s, index - 1, cache)
14+
if index >= 2 and int(s[starting_index:starting_index + 2]) <= 26:
15+
result += helper(s, index - 2, cache)
16+
cache[index] = result
17+
return result
18+
19+
return helper(s, len(s), {})

0 commit comments

Comments
 (0)