leetcode 115. 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).
Here is an example:
S = "rabbbit", T = "rabbit"
Return 3.
/** * @param {string} s * @param {string} t * @return {number} */ var numDistinct = function(s, t) { var M = s.length+1,N = t.length+1; var dp = new Array(N); console.log(dp) for (var i = 0; i < N; i++) { dp[i] = new Array(M); dp[i][0] = 0; } console.log(dp) for (var i = 0; i < M; i++) { dp[0][i] = 1; } console.log(dp) for(let i = 0; i < t.length; i++) { for (var j = 0; j < s.length; j++) { if (s[j] == t[i]) { dp[i+1][j+1] = dp[i][j] + dp[i+1][j]; } else { dp[i+1][j+1] = dp[i+1][j]; } } } return dp[t.length][s.length] };

浙公网安备 33010602011771号