Skip to content

Commit f4eefcc

Browse files
chore: add LeetCode daily solution
1 parent e3853e3 commit f4eefcc

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Number of Substrings With Only 1s (Medium)
2+
3+
**Problem ID:** 1513
4+
**Date:** 2025-11-16
5+
**Link:** https://leetcode.com/problems/number-of-substrings-with-only-1s/
6+
7+
## Approach
8+
9+
To solve the problem of counting the number of substrings consisting solely of '1's in a given binary string, we can adopt a straightforward approach that leverages the properties of contiguous segments of '1's.
10+
11+
### Approach:
12+
13+
1. **Identify Segments of '1's**:
14+
- Traverse the binary string and count the lengths of contiguous segments of '1's. Each time we encounter a '0', we can conclude the current segment of '1's and start counting a new segment if we find another '1'.
15+
16+
2. **Count Substrings for Each Segment**:
17+
- For a segment of length `k` (i.e., `k` consecutive '1's), the number of substrings that can be formed is given by the formula:
18+
\[
19+
\text{Number of substrings} = \frac{k \times (k + 1)}{2}
20+
\]
21+
- This formula arises because:
22+
- There are `k` substrings of length 1 (each individual '1'),
23+
- `k-1` substrings of length 2 (each pair of consecutive '1's),
24+
- and so forth, up to 1 substring of length `k` (the entire segment).
25+
26+
3. **Aggregate the Counts**:
27+
- As we identify each segment of '1's, we compute the number of substrings for that segment using the formula above and maintain a running total.
28+
29+
4. **Handle Large Numbers**:
30+
- Since the result can be very large, we take the total modulo \(10^9 + 7\) at each step to ensure we do not encounter overflow and adhere to the problem's requirements.
31+
32+
### Data Structures:
33+
- We primarily use simple integer variables to track the current segment length and the total count of substrings. No complex data structures are necessary.
34+
35+
### Complexity:
36+
- **Time Complexity**: O(n), where n is the length of the binary string. We make a single pass through the string to count segments and compute the number of substrings.
37+
- **Space Complexity**: O(1), as we use a constant amount of extra space regardless of the input size.
38+
39+
This approach is efficient and straightforward, ensuring that we can handle the upper limits of the input constraints effectively.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int numSub(String s) {
3+
long count = 0;
4+
long current = 0;
5+
final int MOD = 1000000007;
6+
7+
for (char c : s.toCharArray()) {
8+
if (c == '1') {
9+
current++;
10+
count = (count + current) % MOD;
11+
} else {
12+
current = 0;
13+
}
14+
}
15+
16+
return (int) count;
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var numSub = function(s) {
2+
const MOD = 1e9 + 7;
3+
let count = 0, result = 0;
4+
5+
for (let char of s) {
6+
if (char === '1') {
7+
count++;
8+
result = (result + count) % MOD;
9+
} else {
10+
count = 0;
11+
}
12+
}
13+
14+
return result;
15+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def numSub(self, s: str) -> int:
3+
count = 0
4+
total = 0
5+
mod = 10**9 + 7
6+
7+
for char in s:
8+
if char == '1':
9+
count += 1
10+
total = (total + count) % mod
11+
else:
12+
count = 0
13+
14+
return total

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
280280
- 2025-11-13 — [Maximum Number of Operations to Move Ones to the End](https://leetcode.com/problems/maximum-number-of-operations-to-move-ones-to-the-end/) (Medium) → `Medium/2025-11-13-3228-Maximum-Number-of-Operations-to-Move-Ones-to-the-End`
281281
- 2025-11-14 — [Increment Submatrices by One](https://leetcode.com/problems/increment-submatrices-by-one/) (Medium) → `Medium/2025-11-14-2536-Increment-Submatrices-by-One`
282282
- 2025-11-15 — [Count the Number of Substrings With Dominant Ones](https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/) (Medium) → `Medium/2025-11-15-3234-Count-the-Number-of-Substrings-With-Dominant-Ones`
283+
- 2025-11-16 — [Number of Substrings With Only 1s](https://leetcode.com/problems/number-of-substrings-with-only-1s/) (Medium) → `Medium/2025-11-16-1513-Number-of-Substrings-With-Only-1s`

0 commit comments

Comments
 (0)