Leetcode Distinct subsequences-----java

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 .

The problem is that the string T is the number of all possibilities of string s strings (no existence is 0).

At the beginning of the calculation is more violent, so timed out.

 Public classSolution {Char[] word1; Char[] word2; intresult = 0;  Public intnumdistinct (string s, String t) {intLen1 = S.length (), len2 =t.length (); if(Len1 <len2)return0; Word1=S.tochararray (); Word2=T.tochararray ();  for(inti = 0;i<=len1-len2;i++){            if(Word1[i] = = Word2[0]) Helper (i+1,1); }        returnresult; }     Public voidHelperintStart1,intStart2) {                if(Start2 = =word2.length) {Result++; return ; }         for(inti = start1;i< word1.length; i++){            if(Word1[i] = =Word2[start2]) helper (i+1,start2+1); }                            }}

So use the DP algorithm.

DP, the method of normalization of two-dimensional map.

R a b b i t

1 0 0 0 0 0 0

R 1

A 1

B 1

B 1

B 1

I 1

T 1

If the current character is the same, dp[i][j] result equals (Dp[i-1][j-1]) and (Dp[i-1][j]) sum

If the current character is different, dp[i][j] = Dp[i-1][j]

 Public classSolution { Public intnumdistinct (string s, String t) {intLen1 = S.length (), len2 =t.length (); if(Len1 <len2)return0; Char[] Word1 =S.tochararray (); Char[] Word2 =T.tochararray (); int[] DP =New int[Len1+1] [Len2+1];  for(inti = 0;i<=len1;i++){             for(intj = 0;j<=i && j<=len2;j++){                        if(i = = 0 && J! = 0) Dp[i][j]= 0; Else if(j = = 0) Dp[i][j]= 1; Else if(Word1[i-1] = = Word2[j-1]) Dp[i][j]= Dp[i-1][j-1] + dp[i-1][j]; ElseDp[i][j]= Dp[i-1][j]; }        }            returnDp[len1][len2]; }            }

Leetcode Distinct subsequences-----java

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.