Distinct Subsequences
Problem Statement
Given two strings s and t, return the number of distinct subsequences of s
which equals t
.
The test cases are generated so that the answer fits on a 32-bit signed integer.
Example 1:
Input: s = "rabbbit", t = "rabbit"
Output: 3
Example 2:
Input: s = "babgbag", t = "bag"
Output: 5
Constraints:
1 <= s.length, t.length <= 1000
s
andt
consist of English letters.
Code
Python Code
class Solution:
def numDistinct(self, s, t):
dp = [[0] * (len(s) + 1) for _ in range(len(t) + 1)]
for col in range(len(dp[0])):
dp[0][col] = 1
for x in range(1, len(s) + 1):
for y in range(1, len(t) + 1):
if s[x - 1] == t[y - 1]:
dp[y][x] = dp[y - 1][x - 1] + dp[y][x - 1]
else:
dp[y][x] = dp[y][x - 1]
return dp[-1][-1]
Java Code
public int numDistinct(String S, String T) {
// array creation
int[][] mem = new int[T.length()+1][S.length()+1];
// filling the first row: with 1s
for(int j=0; j<=S.length(); j++) {
mem[0][j] = 1;
}
// the first column is 0 by default in every other rows but the first, which we need.
for(int i=0; i<T.length(); i++) {
for(int j=0; j<S.length(); j++) {
if(T.charAt(i) == S.charAt(j)) {
mem[i+1][j+1] = mem[i][j] + mem[i+1][j];
} else {
mem[i+1][j+1] = mem[i+1][j];
}
}
}
return mem[T.length()][S.length()];
}