Merge pull request #495 from jackeyjia/patch-15

add js solution for numDistinct
This commit is contained in:
程序员Carl
2021-07-17 16:21:13 +08:00
committed by GitHub

View File

@ -222,7 +222,28 @@ class SolutionDP2:
Go
Javascript:
```javascript
const numDistinct = (s, t) => {
let dp = Array.from(Array(s.length + 1), () => Array(t.length +1).fill(0));
for(let i = 0; i <=s.length; i++) {
dp[i][0] = 1;
}
for(let i = 1; i <= s.length; i++) {
for(let j = 1; j<= t.length; j++) {
if(s[i-1] === t[j-1]) {
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
} else {
dp[i][j] = dp[i-1][j]
}
}
}
return dp[s.length][t.length];
};
```
-----------------------