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.
题意有点歧义。其实是要求S中有多少个不同的子串等于T。
dfs的思路很好写,就是从s查到*t,如果找到了,再从s+1中去找*(t+1),直到最后t为空。
1 class Solution { 2 public: 3 int numDistinct(string S, string T) { 4 count = 0; 5 dfs(S.c_str(), T.c_str()); 6 return count; 7 } 8 9 void dfs(const char* s, const char* t) { 10 if (*s == '