Skip to content

Commit e3853e3

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

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Count the Number of Substrings With Dominant Ones (Medium)
2+
3+
**Problem ID:** 3234
4+
**Date:** 2025-11-15
5+
**Link:** https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/
6+
7+
## Approach
8+
9+
To solve the problem of counting the number of substrings with dominant ones in a binary string, we can adopt a systematic approach that leverages the properties of the substring counts and the conditions for dominance.
10+
11+
### Main Idea:
12+
A substring is considered to have dominant ones if the number of ones (`count_1`) is greater than or equal to the square of the number of zeros (`count_0`). This can be expressed mathematically as:
13+
\[ count_1 \geq count_0^2 \]
14+
15+
Given this condition, we can derive a way to efficiently count valid substrings by iterating through the string and maintaining a running count of zeros and ones.
16+
17+
### Approach:
18+
1. **Prefix Counts**: As we iterate through the string, we maintain cumulative counts of zeros and ones. This allows us to quickly calculate the number of zeros and ones in any substring defined by indices `i` and `j`.
19+
20+
2. **Two-pointer Technique**: For each starting index `i`, we can use a second pointer `j` to extend the substring while checking the dominance condition. We will increment `j` until the condition fails, keeping track of valid substrings.
21+
22+
3. **Count Valid Substrings**: For each valid starting index `i`, once we find the maximum index `j` where the condition holds, all substrings from `i` to any index up to `j` are valid. Thus, if `j` is the farthest index satisfying the condition, we can add `(j - i + 1)` to our count of dominant substrings.
23+
24+
4. **Complexity Consideration**: The algorithm primarily involves iterating through the string with two pointers, leading to a time complexity of O(n), where n is the length of the string. This is efficient given the constraints.
25+
26+
### Data Structures:
27+
- Two integers to keep track of the cumulative counts of zeros and ones.
28+
- A loop to iterate through the string, with a nested loop (or a two-pointer approach) to find valid substrings.
29+
30+
### Complexity:
31+
- **Time Complexity**: O(n), where n is the length of the string. Each character is processed a limited number of times.
32+
- **Space Complexity**: O(1) for maintaining counts, as we only need a fixed number of variables regardless of input size.
33+
34+
By following this structured approach, we can efficiently count the number of dominant substrings in the binary string provided.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int countSubstrings(String s) {
3+
int n = s.length();
4+
int count = 0;
5+
6+
for (int i = 0; i < n; i++) {
7+
int ones = 0, zeros = 0;
8+
for (int j = i; j < n; j++) {
9+
if (s.charAt(j) == '1') {
10+
ones++;
11+
} else {
12+
zeros++;
13+
}
14+
if (ones >= zeros * zeros) {
15+
count++;
16+
}
17+
}
18+
}
19+
20+
return count;
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var countSubstrings = function(s) {
2+
let n = s.length;
3+
let count = 0;
4+
5+
for (let i = 0; i < n; i++) {
6+
let ones = 0, zeros = 0;
7+
for (let j = i; j < n; j++) {
8+
if (s[j] === '1') {
9+
ones++;
10+
} else {
11+
zeros++;
12+
}
13+
if (ones >= zeros * zeros) {
14+
count++;
15+
}
16+
}
17+
}
18+
19+
return count;
20+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def countSubstrings(self, s: str) -> int:
3+
n = len(s)
4+
total_substrings = n * (n + 1) // 2
5+
non_dominant_count = 0
6+
7+
for i in range(n):
8+
zeros = 0
9+
ones = 0
10+
for j in range(i, n):
11+
if s[j] == '0':
12+
zeros += 1
13+
else:
14+
ones += 1
15+
if ones < zeros * zeros:
16+
non_dominant_count += 1
17+
18+
return total_substrings - non_dominant_count

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
279279
- 2025-11-12 — [Minimum Number of Operations to Make All Array Elements Equal to 1](https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/) (Medium) → `Medium/2025-11-12-2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1`
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`
282+
- 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`

0 commit comments

Comments
 (0)