Skip to content

Commit 925926d

Browse files
authored
Decode_Ways(Recursion)
Initial File
1 parent 3f64779 commit 925926d

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Decode_Ways(Recursion)

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
RECURSION
2+
3+
4+
def numDecodings(A,index):
5+
#when character is empty string
6+
if index==len(A):
7+
return 1
8+
#when only character is 0
9+
if A[index] == "0":
10+
return 0
11+
#when single character
12+
if index == len(A)-1:
13+
return 1
14+
way1=numDecodings(A,index+1)
15+
way2=0
16+
if int(A[index:index+2]) <=26:
17+
way2=numDecodings(A,index+2)
18+
return way1+way2
19+
20+
21+
22+
23+
24+
MEMOIZATION
25+
26+
A="1112"
27+
arr= [0 for i in range(len(A)+1)]
28+
def numDecodings_memo(A,index,arr):
29+
#when character is empty string
30+
if index==len(A):
31+
return 1
32+
#when only character is 0
33+
if A[index] == "0":
34+
return 0
35+
#when single character
36+
if index == len(A)-1:
37+
return 1
38+
if arr[index]>0:
39+
return arr[index]
40+
way1=numDecodings_memo(A,index+1,arr)
41+
way2=0
42+
if int(A[index:index+2]) <=26:
43+
way2=numDecodings_memo(A,index+2,arr)
44+
arr[index]= way1+way2
45+
return arr[index]
46+
47+
48+
print(numDecodings_memo(A,0,arr))
49+

0 commit comments

Comments
 (0)