leetcode_distinct subsequences

Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ACE” is a subsequence of “ABCDE” while “AEC” is not).
(有效子序列个数)

Example:

1. 动态规划

这个题也可以看做一个字符串匹配的问题,一般都可以使用动态规划求解。具体实现过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class :
def numDistinct(self, s: str, t: str) -> int:

if not t:
return 1
# t 不为空,s 为空,不可能匹配。
if not s:
return 0

m, n = len(s), len(t)
dp = [[0]* (n + 1) for _ in range(m + 1)]

for i in range(m+1):
for j in range(n+1):
if i == 0 and j == 0:
dp[i][j] = 1
elif i == 0:
dp[i][j] = 0
elif j == 0:
dp[i][j] = 1
else:
# s[i-1] 不参与匹配
dp[i][j] = dp[i-1][j]
# s[i-1] 和 t[j-1] 匹配
if s[i-1] == t[j-1]:
dp[i][j] += dp[i-1][j-1]

return dp[m][n]