Leetcode:distinct subsequences

Source: Internet
Author: User

Distinct subsequences


Total Accepted: 51556 Total Submissions: 177996 Difficulty: Hard

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, " ACE "  is a subsequence of " ABCDE "  while " AEC "  is not).

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

Return 3 .

Subscribe to see which companies asked this question

Hide TagsDynamic Programming String




















Test instructions: The possible transformation of s to T.


Idea: Dynamic regulation

Set: S = "Rabbbit", t= "Rabbit"

Dp[t.length () +1][s.length () +1];

Manual calculation can be done by:


J 0 1 2 3 4 5 6 7    

I S R a b b b i t

0 T 1 1 1 1 1 1 1 1

1 r 0 1 1 1 1 1 1 1

2 a 0 0 1 1 1 1 1 1

3 b 0 0 0 1 2 3 3 3

4 b 0 0 00 1 3 3 3

5 I 0 0 0 0 0 03 3

6 t 0 0 0 0 0 0 03


The result is: 3 (==dp[t.length ()][s.length ()]);

Observe the above DP the number of dashes in the table, the generation process can be obtained:

if (t[i] = = S[j]) dp[i][j] = Dp[i-1][j-1] + dp[i][j-1];

else dp[i][j] = dp[i][j-1];


That

When t[i] = = S[j], the current character can be preserved or discarded;

When t[i]! = S[j], the current character can only be discarded.


When I==0, the T is empty, when there is only one transformation possible, that is, remove all the characters in S.


Java code:

public class Solution {public    int numdistinct (string s, String t) {                int m = T.length ();        int n = s.length ();                Int[][] dp = new INT[M+1][N+1];                for (int i=0;i<=m;i++) dp[i][0] = 0;        for (int j=0;j<=n;j++) dp[0][j] = 1;                for (int i=1;i<=m;i++) {for            (int j=1;j<=n;j++) {                if (S.charat (j-1) ==t.charat (i-1))                    dp[i][j] = dp[i-1 ][J-1] + dp[i][j-1];                else                    dp[i][j] = dp[i][j-1];            }        }                return dp[m][n];}    }


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.