From ae8a0f29f2401c5749c700394e24ffcdc67be1fc Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Fri, 16 Jul 2021 22:08:38 -0700 Subject: [PATCH] add js solution for numDistinct --- problems/0115.不同的子序列.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md index 62af9d0f..014eb3cd 100644 --- a/problems/0115.不同的子序列.md +++ b/problems/0115.不同的子序列.md @@ -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]; +}; +``` -----------------------