Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bd73665

Browse files
committed
Add solution #1987
1 parent 95ce1f6 commit bd73665

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,671 LeetCode solutions in JavaScript
1+
# 1,672 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1525,6 +1525,7 @@
15251525
1984|[Minimum Difference Between Highest and Lowest of K Scores](./solutions/1984-minimum-difference-between-highest-and-lowest-of-k-scores.js)|Easy|
15261526
1985|[Find the Kth Largest Integer in the Array](./solutions/1985-find-the-kth-largest-integer-in-the-array.js)|Medium|
15271527
1986|[Minimum Number of Work Sessions to Finish the Tasks](./solutions/1986-minimum-number-of-work-sessions-to-finish-the-tasks.js)|Medium|
1528+
1987|[Number of Unique Good Subsequences](./solutions/1987-number-of-unique-good-subsequences.js)|Hard|
15281529
1996|[The Number of Weak Characters in the Game](./solutions/1996-the-number-of-weak-characters-in-the-game.js)|Medium|
15291530
2000|[Reverse Prefix of Word](./solutions/2000-reverse-prefix-of-word.js)|Easy|
15301531
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 1987. Number of Unique Good Subsequences
3+
* https://leetcode.com/problems/number-of-unique-good-subsequences/
4+
* Difficulty: Hard
5+
*
6+
* You are given a binary string binary. A subsequence of binary is considered good if it is not
7+
* empty and has no leading zeros (with the exception of "0").
8+
*
9+
* Find the number of unique good subsequences of binary.
10+
* - For example, if binary = "001", then all the good subsequences are ["0", "0", "1"], so the
11+
* unique good subsequences are "0" and "1". Note that subsequences "00", "01", and "001" are
12+
* not good because they have leading zeros.
13+
*
14+
* Return the number of unique good subsequences of binary. Since the answer may be very large,
15+
* return it modulo 109 + 7.
16+
*
17+
* A subsequence is a sequence that can be derived from another sequence by deleting some or no
18+
* elements without changing the order of the remaining elements.
19+
*/
20+
21+
/**
22+
* @param {string} binary
23+
* @return {number}
24+
*/
25+
var numberOfUniqueGoodSubsequences = function(binary) {
26+
const MOD = 1e9 + 7;
27+
let endsWithZero = 0;
28+
let endsWithOne = 0;
29+
let hasZero = false;
30+
31+
for (const digit of binary) {
32+
if (digit === '0') {
33+
hasZero = true;
34+
endsWithZero = (endsWithZero + endsWithOne) % MOD;
35+
} else {
36+
endsWithOne = (endsWithOne + endsWithZero + 1) % MOD;
37+
}
38+
}
39+
40+
return (endsWithZero + endsWithOne + (hasZero ? 1 : 0)) % MOD;
41+
};

0 commit comments

Comments
 (0)