Skip to content

Commit 781656c

Browse files
committed
feat(#0115): add Distinct Subsequences
1 parent 954c7e9 commit 781656c

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

0115. Distinct Subsequences.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,42 @@
4040
// babgbag
4141
// ^^^
4242

43+
// b a g
44+
// 0 1 2 3
45+
// 0 1 0 0 0
46+
// b 1 1 1 0 0
47+
// a 2 1 1 1 0
48+
// b 3 1 2 1 0
49+
// g 4 1 2 1 1
50+
// b 5 1 3 1 1
51+
// a 6 1 3 4 1
52+
// g 7 1 3 4 5
53+
4354
/**
4455
* @param {string} s
4556
* @param {string} t
4657
* @return {number}
4758
*/
48-
const numDistinct = (s, t) => {}
59+
const numDistinct = (s, t) => {
60+
const sLen = s.length
61+
const tLen = t.length
62+
const dp = [...Array(tLen + 1)].map(() => Array(sLen + 1).fill(0))
63+
for (let j = 0; j < sLen; j++) {
64+
dp[0][j] = 1
65+
}
66+
for (let i = 0; i < tLen; i++) {
67+
for (let j = 0; j < sLen; j++) {
68+
if (t[i] === s[j]) {
69+
dp[i + 1][j + 1] = dp[i][j] + dp[i + 1][j]
70+
} else {
71+
dp[i + 1][j + 1] = dp[i + 1][j]
72+
}
73+
}
74+
}
75+
return dp[tLen][sLen]
76+
}
77+
78+
// test case:
79+
const s = 'babgbag'
80+
const t = 'bag'
81+
console.log(numDistinct(s, t))

package.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "leetcode",
3+
"version": "1.0.0",
4+
"description": "记录 LeetCode 解题历程",
5+
"main": "leetcode",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"lint": "eslint .",
9+
"lint:fix": "eslint . --fix"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/zangbianxuegu/LeetCode.git"
14+
},
15+
"author": "",
16+
"license": "ISC",
17+
"bugs": {
18+
"url": "https://github.com/zangbianxuegu/LeetCode/issues"
19+
},
20+
"homepage": "https://github.com/zangbianxuegu/LeetCode#readme",
21+
"eslintConfig": {
22+
"extends": [
23+
"wesbos"
24+
],
25+
"rules": {
26+
"no-console": 0,
27+
"prettier/prettier": [
28+
"error",
29+
{
30+
"semi": false,
31+
"trailingComma": "es5",
32+
"singleQuote": true,
33+
"printWidth": 200,
34+
"tabWidth": 2
35+
}
36+
]
37+
}
38+
},
39+
"devDependencies": {
40+
"babel-eslint": "^9.0.0",
41+
"eslint": "^5.16.0",
42+
"eslint-config-airbnb": "^17.1.1",
43+
"eslint-config-prettier": "^4.3.0",
44+
"eslint-config-wesbos": "0.0.19",
45+
"eslint-plugin-html": "^5.0.5",
46+
"eslint-plugin-import": "^2.20.2",
47+
"eslint-plugin-jsx-a11y": "^6.2.3",
48+
"eslint-plugin-prettier": "^3.1.3",
49+
"eslint-plugin-react": "^7.19.0",
50+
"eslint-plugin-react-hooks": "^1.7.0",
51+
"prettier": "^1.19.1"
52+
}
53+
}

0 commit comments

Comments
 (0)