File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments