Skip to content

Commit cdf78c2

Browse files
authored
Create distinct_subsequences_dp.py
1 parent 9501e45 commit cdf78c2

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+
15+
C = [[0 for _ in xrange(n+1)] for _ in xrange(m+1)]
16+
for j in xrange(n+1):
17+
C[0][j] = 0
18+
for i in xrange(m+1):
19+
C[i][0] = 1
20+
21+
for i in xrange(1, m+1):
22+
for j in xrange(1, n+1):
23+
if S[i-1]==T[j-1]:
24+
C[i][j] = C[i-1][j]+C[i-1][j-1]
25+
else:
26+
C[i][j] = C[i-1][j]
27+
28+
return C[m][n]
29+
30+
print distinct_subsequences("abadcb", "ab")

0 commit comments

Comments
 (0)