[Leetcode] Distinct subsequences

Source: Internet
Author: User

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 was formed from the original string by deleting some (can be none) of the C Haracters without disturbing the relative positions of the remaining characters. (ie, is a subsequence of and is not "ACE" "ABCDE" "AEC" ).

Here are an example:
S = "rabbbit" , T ="rabbit"

Return 3 .

Hide TagsDynamic Programming StringMethod One: Dfs, string by case comparison, big data timeout,
classSolution {intm_cnt; Public:    voidDfsConst string& S,Const string& T,intIDX1,intidx2) {           if(IDX2 = =t.size ()) {m_cnt++; return; }           if(Idx1 = =s.size ())return;  for(inti = idx1; I < s.size (); i++)        {               if(S[i] = =t[idx2]) Dfs (s, T, I+1, idx2+1); }       }       intNumdistinct (stringSstringT) {m_cnt=0; DFS (S,t,0,0); returnm_cnt; }   };

Method Two: DP

Set the status to F (i, j), indicating the number of occurrences of t[0,i-1] in s[0,j-1].

If T[i]! = S[j], then f[i][j] = f[i][j-1] means the same number of occurrences as t[0,i-1] in s[0,j-1].

If t[i] = = S[j], then f[i][j] = f[i][j-1] + f[i-1][j-1]

Two methods: T[i] is derived from s[j], there are f[i-1][j-1] species

T[i] is not derived from s[j], but from s[0~i-1], there are f[i][j-1] species

Time complexity O (m*n), Space O (m*n), but can be optimized to O (m) using a scrolling array
classSolution { Public:    intNumdistinct (stringSstringt) {vector<int> tmp (s.size () +1,0); Vector<vector<int> > F (t.size () +1, TMP); //F[i][j] indicates nums that t[0,i-1] is converted by s[0,j-1]f[0][0] =1;//null to NULL with only one         for(inti =1; I < s.size (); i++)//There is the only one-on-one-one-out-of- string changed to nullf[0][i] =1;  for(inti =1; I < t.size (); i++)//There is no -it-that null-to-any stringf[i][0] =0;  for(intj =1; J <= S.size (); J + +)        {             for(inti =1; I <= t.size (); i++)            {                if(I >j) F[i][j]=0; Else                {                    if(t[i-1] = = s[j-1]) F[i][j]= f[i-1][j-1] + f[i][j-1]; ElseF[i][j]= f[i][j-1]; }            }        }        returnf[t.size ()][s.size ()];       }}; 

[Leetcode] Distinct subsequences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.