Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of S which equals T.

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).

It's guaranteed the answer fits on a 32-bit signed integer.

Example 1:

Input: S = "rabbbit", T = "rabbit"
Output: 3
Explanation:
As shown below, there are 3 ways you can generate "rabbit" from S.
(The caret symbol ^ means the chosen letters)

rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^

Example 2:

Input: S = "babgbag", T = "bag"
Output: 5
Explanation:
As shown below, there are 5 ways you can generate "bag" from S.
(The caret symbol ^ means the chosen letters)

babgbag
^^ ^
babgbag
^^    ^
babgbag
^    ^^
babgbag
  ^  ^^
babgbag
    ^^^
class Solution {
    public int numDistinct(String s, String t) {
        int m = s.length(), n = t.length();
        int[][] dp = new int[m + 1][n + 1];
        // initialize the dp value when t is an empty string, number of subsequence of
        // an empty string should be 1
        for (int i = 0; i < m; i++) {
            dp[i][0] = 1; // Base case
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                // in both cases, the subsequence in String t should be ending with character
                // t.charAt(j - 1)
                if (s.charAt(i - 1) == t.charAt(j - 1)) {
                    // when two pointers pointing to same character
                    // if we take these two characters simultaneously, we should have dp[i-1][j-1]
                    // subsequences
                    // otherwise if we overlook current i (moving back for one step) and keeping the
                    // current j we have another dp[i -1][j]
                    dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
                } else {
                    // when two pointers pointing to difference characters
                    // we cannot take these two characters but we still should make j ending with
                    // pointing to current position
                    // then we should move i backward
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        return dp[m][n];
    }
}

Last updated