Skip to content

Commit 3a92604

Browse files
committed
Add solution #115
1 parent c4e12a6 commit 3a92604

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
112|[Path Sum](./0112-path-sum.js)|Easy|
110110
113|[Path Sum II](./0113-path-sum-ii.js)|Medium|
111111
114|[Flatten Binary Tree to Linked List](./0114-flatten-binary-tree-to-linked-list.js)|Medium|
112+
115|[Distinct Subsequences](./0115-distinct-subsequences.js)|Hard|
112113
116|[Populating Next Right Pointers in Each Node](./0116-populating-next-right-pointers-in-each-node.js)|Medium|
113114
118|[Pascal's Triangle](./0118-pascals-triangle.js)|Easy|
114115
119|[Pascal's Triangle II](./0119-pascals-triangle-ii.js)|Easy|

Diff for: solutions/0115-distinct-subsequences.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 115. Distinct Subsequences
3+
* https://leetcode.com/problems/distinct-subsequences/
4+
* Difficulty: Hard
5+
*
6+
* Given two strings s and t, return the number of distinct subsequences of s which equals t.
7+
*
8+
* The test cases are generated so that the answer fits on a 32-bit signed integer.
9+
*/
10+
11+
/**
12+
* @param {string} s
13+
* @param {string} t
14+
* @return {number}
15+
*/
16+
var numDistinct = function(s, t) {
17+
const nums = new Array(t.length + 1).fill(0);
18+
nums[0] = 1;
19+
20+
for (let i = 0; i < s.length; i++) {
21+
for (let j = nums.length - 1; j >= 0; j--) {
22+
if (s[i] === t[j]) {
23+
nums[j + 1] += nums[j];
24+
}
25+
}
26+
}
27+
28+
return nums[t.length];
29+
};

0 commit comments

Comments
 (0)