Given a string S and a string T, count the number of distinct subsequences of T in S.
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).
Here is an example:
S = "rabbbit"
, T = "rabbit"
Return 3
.
典型动态规划问题,(下一个状态受到 前一个状态的影响)
class Solution:
def resolveDistinct(self,sub,target):
list1=[0 ft _ in xrange(len(T)+1)]
list1[0]=1
for j in sub:
for k,v in reversed(list(enumerate(target))):
if i==v:
list1[k+1]+=list2[k]
return list1[len(target)]