Skip to content

Commit 718c1fc

Browse files
committed
Distinct subsequences solution
1 parent 73347f3 commit 718c1fc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

115_distinct_subsequences.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def numDistinct(self, s, t):
3+
"""
4+
:type s: str
5+
:type t: str
6+
:rtype: int
7+
"""
8+
# https://discuss.leetcode.com/topic/51131/space-o-mn-and-o-n-python-solutions
9+
dp = [[0 for j in xrange(0, len(t) + 1)] for i in xrange(0, len(s) + 1)]
10+
for j in xrange(1, len(t) + 1):
11+
dp[0][j] = 0
12+
for i in xrange(1, len(s) + 1):
13+
dp[i][0] = 1
14+
dp[0][0] = 1
15+
for i in xrange(1, len(s) + 1):
16+
for j in xrange(1, len(t) + 1):
17+
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] * (s[i - 1] == t[j - 1])
18+
19+
return dp[-1][-1]

0 commit comments

Comments
 (0)