File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 109109112|[ Path Sum] ( ./0112-path-sum.js ) |Easy|
110110113|[ Path Sum II] ( ./0113-path-sum-ii.js ) |Medium|
111111114|[ Flatten Binary Tree to Linked List] ( ./0114-flatten-binary-tree-to-linked-list.js ) |Medium|
112+ 115|[ Distinct Subsequences] ( ./0115-distinct-subsequences.js ) |Hard|
112113116|[ Populating Next Right Pointers in Each Node] ( ./0116-populating-next-right-pointers-in-each-node.js ) |Medium|
113114118|[ Pascal's Triangle] ( ./0118-pascals-triangle.js ) |Easy|
114115119|[ Pascal's Triangle II] ( ./0119-pascals-triangle-ii.js ) |Easy|
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments