Skip to content

Commit 4d25268

Browse files
committed
Add solution #1513
1 parent 2362ec7 commit 4d25268

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-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,339 LeetCode solutions in JavaScript
1+
# 1,340 LeetCode solutions in JavaScript
22

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

@@ -1156,6 +1156,7 @@
11561156
1509|[Minimum Difference Between Largest and Smallest Value in Three Moves](./solutions/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.js)|Medium|
11571157
1510|[Stone Game IV](./solutions/1510-stone-game-iv.js)|Hard|
11581158
1512|[Number of Good Pairs](./solutions/1512-number-of-good-pairs.js)|Easy|
1159+
1513|[Number of Substrings With Only 1s](./solutions/1513-number-of-substrings-with-only-1s.js)|Medium|
11591160
1519|[Number of Nodes in the Sub-Tree With the Same Label](./solutions/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.js)|Medium|
11601161
1524|[Number of Sub-arrays With Odd Sum](./solutions/1524-number-of-sub-arrays-with-odd-sum.js)|Medium|
11611162
1528|[Shuffle String](./solutions/1528-shuffle-string.js)|Easy|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 1513. Number of Substrings With Only 1s
3+
* https://leetcode.com/problems/number-of-substrings-with-only-1s/
4+
* Difficulty: Medium
5+
*
6+
* Given a binary string s, return the number of substrings with all characters 1's. Since the
7+
* answer may be too large, return it modulo 109 + 7.
8+
*/
9+
10+
/**
11+
* @param {string} s
12+
* @return {number}
13+
*/
14+
var numSub = function(s) {
15+
const MOD = 1e9 + 7;
16+
let result = 0;
17+
let consecutiveOnes = 0;
18+
19+
for (const char of s) {
20+
if (char === '1') {
21+
consecutiveOnes++;
22+
result = (result + consecutiveOnes) % MOD;
23+
} else {
24+
consecutiveOnes = 0;
25+
}
26+
}
27+
28+
return result;
29+
};

0 commit comments

Comments
 (0)