Skip to content

Commit 56eae9c

Browse files
authored
Decode_Ways(DP)
Initial File
1 parent 925926d commit 56eae9c

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Decode_Ways(DP)

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def numDecodings(A):
2+
if len(A) == 0:
3+
return 0
4+
if int(A[0]) == 0:
5+
return 0
6+
n = len(A)
7+
result = [0 for _ in range(0, n + 1)]
8+
result[0] = result[1] = 1
9+
for i in range(1, n):
10+
v1 = int(A[i:i + 1])
11+
v2 = int(A[i - 1:i + 1])
12+
if 0 < v1 <= 9:
13+
result[i + 1] = result[i]
14+
if 10 <= v2 <= 26:
15+
result[i + 1] = result[i + 1] + result[i - 1]
16+
if result[i + 1] == 0:
17+
return 0
18+
19+
answer = result[n]
20+
return answer
21+
22+
print(numDecodings("1204"))

0 commit comments

Comments
 (0)