Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: subsequences

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Thanked 0 Times in 0 Posts

    Default subsequences

    Hi All: can Anybody explain to me in details how you get this solution. I am not getting it. Thanks

    Problem: 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.


    public int numDistincts(String S, String T) {
    int[][] table = new int[S.length() + 1][T.length() + 1];

    for (int i = 0; i < S.length(); i++)
    table[i][0] = 1;

    for (int i = 1; i <= S.length(); i++) {
    for (int j = 1; j <= T.length(); j++) {
    if (S.charAt(i - 1) == T.charAt(j - 1)) {
    table[i][j] += table[i - 1][j] + table[i - 1][j - 1];
    } else {
    table[i][j] += table[i - 1][j];

    return table[S.length()][T.length()];

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: subsequences

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.