LeetCode.392 判断子序列
dp = [[0] * (len(s)+1) for _ in range(len(t)+1)]
for i in range(1, len(dp)):
for j in range(1, len(dp[0])):
dp[i][j] = dp[i-1][j-1] + 1 if s[j-1] == t[i-1] else max(dp[i-1][j], dp[i][j-1])
return dp[-1][-1] == len(s)
LeetCode.115 不同的子序列
dp = [[0] * (len(s) + 1) for _ in range(len(t) + 1)]
for i in range(len(dp[0])):
dp[0][i] = 1
for i in range(1, len(dp)):
for j in range(1, len(dp[0])):
dp[i][j] = dp[i][j-1] + (dp[i-1][j-1] if s[j-1] == t[i-1] else 0)
return dp[-1][-1]