Skip to content

Commit e70378f

Browse files
Merge pull request youngyangyang04#495 from jackeyjia/patch-15
add js solution for numDistinct
2 parents 63b3ede + ae8a0f2 commit e70378f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

problems/0115.不同的子序列.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,28 @@ class SolutionDP2:
222222

223223
Go:
224224

225+
Javascript:
226+
```javascript
227+
const numDistinct = (s, t) => {
228+
let dp = Array.from(Array(s.length + 1), () => Array(t.length +1).fill(0));
225229

230+
for(let i = 0; i <=s.length; i++) {
231+
dp[i][0] = 1;
232+
}
233+
234+
for(let i = 1; i <= s.length; i++) {
235+
for(let j = 1; j<= t.length; j++) {
236+
if(s[i-1] === t[j-1]) {
237+
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
238+
} else {
239+
dp[i][j] = dp[i-1][j]
240+
}
241+
}
242+
}
243+
244+
return dp[s.length][t.length];
245+
};
246+
```
226247

227248

228249
-----------------------

0 commit comments

Comments
 (0)