3234. Count the Number of Substrings With Dominant Ones Medium

@problem@discussion
#String#Sliding Window#Enumeration



1/**
2 * [3234] Count the Number of Substrings With Dominant Ones
3 *
4 * You are given a binary string s.
5 * Return the number of <span data-keyword="substring-nonempty">substrings</span> with dominant ones.
6 * A string has dominant ones if the number of ones in the string is greater than or equal to the square of the number of zeros in the string.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">s = "00011"</span>
11 * Output: <span class="example-io">5</span>
12 * Explanation:
13 * The substrings with dominant ones are shown in the table below.
14 * </div>
15 * <table>
16 * 	<thead>
17 * 		<tr>
18 * 			<th>i</th>
19 * 			<th>j</th>
20 * 			<th>s[i..j]</th>
21 * 			<th>Number of Zeros</th>
22 * 			<th>Number of Ones</th>
23 * 		</tr>
24 * 	</thead>
25 * 	<tbody>
26 * 		<tr>
27 * 			<td>3</td>
28 * 			<td>3</td>
29 * 			<td>1</td>
30 * 			<td>0</td>
31 * 			<td>1</td>
32 * 		</tr>
33 * 		<tr>
34 * 			<td>4</td>
35 * 			<td>4</td>
36 * 			<td>1</td>
37 * 			<td>0</td>
38 * 			<td>1</td>
39 * 		</tr>
40 * 		<tr>
41 * 			<td>2</td>
42 * 			<td>3</td>
43 * 			<td>01</td>
44 * 			<td>1</td>
45 * 			<td>1</td>
46 * 		</tr>
47 * 		<tr>
48 * 			<td>3</td>
49 * 			<td>4</td>
50 * 			<td>11</td>
51 * 			<td>0</td>
52 * 			<td>2</td>
53 * 		</tr>
54 * 		<tr>
55 * 			<td>2</td>
56 * 			<td>4</td>
57 * 			<td>011</td>
58 * 			<td>1</td>
59 * 			<td>2</td>
60 * 		</tr>
61 * 	</tbody>
62 * </table>
63 * <strong class="example">Example 2:
64 * <div class="example-block">
65 * Input: <span class="example-io">s = "101101"</span>
66 * Output: <span class="example-io">16</span>
67 * Explanation:
68 * The substrings with non-dominant ones are shown in the table below.
69 * Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.
70 * </div>
71 * <table>
72 * 	<thead>
73 * 		<tr>
74 * 			<th>i</th>
75 * 			<th>j</th>
76 * 			<th>s[i..j]</th>
77 * 			<th>Number of Zeros</th>
78 * 			<th>Number of Ones</th>
79 * 		</tr>
80 * 	</thead>
81 * 	<tbody>
82 * 		<tr>
83 * 			<td>1</td>
84 * 			<td>1</td>
85 * 			<td>0</td>
86 * 			<td>1</td>
87 * 			<td>0</td>
88 * 		</tr>
89 * 		<tr>
90 * 			<td>4</td>
91 * 			<td>4</td>
92 * 			<td>0</td>
93 * 			<td>1</td>
94 * 			<td>0</td>
95 * 		</tr>
96 * 		<tr>
97 * 			<td>1</td>
98 * 			<td>4</td>
99 * 			<td>0110</td>
100 * 			<td>2</td>
101 * 			<td>2</td>
102 * 		</tr>
103 * 		<tr>
104 * 			<td>0</td>
105 * 			<td>4</td>
106 * 			<td>10110</td>
107 * 			<td>2</td>
108 * 			<td>3</td>
109 * 		</tr>
110 * 		<tr>
111 * 			<td>1</td>
112 * 			<td>5</td>
113 * 			<td>01101</td>
114 * 			<td>2</td>
115 * 			<td>3</td>
116 * 		</tr>
117 * 	</tbody>
118 * </table>
119 *  
120 * Constraints:
121 * 
122 * 	1 <= s.length <= 4 * 10^4
123 * 	s consists only of characters '0' and '1'.
124 * 
125 */
126pub struct Solution {}
127
128// problem: https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/
129// discuss: https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/discuss/?currentPage=1&orderBy=most_votes&query=
130
131// submission codes start here
132
133impl Solution {
134    pub fn number_of_substrings(s: String) -> i32 {
135        0
136    }
137}
138
139// submission codes end
140
141#[cfg(test)]
142mod tests {
143    use super::*;
144
145    #[test]
146    fn test_3234() {
147    }
148}
149


Back
© 2025 bowen.ge All Rights Reserved.