Skip to content

Commit 9501e45

Browse files
authored
Create distinct_subsequences_recursive.py
1 parent 9355904 commit 9501e45

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) June 02, 2017 CareerMonk Publications and others.
2+
3+
# Creation Date : 2017-06-02 06:15:46
4+
# Last modification : 2017-06-02
5+
# Modified by : Narasimha Karumanchi
6+
# Book Title : Algorithm Design Techniques
7+
# Warranty : This software is provided "as is" without any
8+
# warranty; without even the implied warranty of
9+
# merchantability or fitness for a particular purpose.
10+
11+
def distinct_subsequences(S, T):
12+
m = len(S)
13+
n = len(T)
14+
# Base conditions
15+
if (m==0):
16+
return 0
17+
if (n==0):
18+
return 1
19+
if (m==1 and n==1):
20+
if (S[0]==T[0]):
21+
return 1
22+
else:
23+
return 0
24+
25+
if (S[m-1]==T[n-1]):
26+
return distinct_subsequences(S[:-1], T[:-1]) + distinct_subsequences(S[:-1], T)
27+
else:
28+
return distinct_subsequences(S[:-1], T)
29+
30+
print distinct_subsequences("abadcb", "ab")

0 commit comments

Comments
 (0)