|
| 1 | +<h2><a href="https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones">3479. Count the Number of Substrings With Dominant Ones</a></h2><h3>Medium</h3><hr><p>You are given a binary string <code>s</code>.</p> |
| 2 | + |
| 3 | +<p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> |
| 4 | + |
| 5 | +<p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>square</strong> of the number of zeros in the string.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<div class="example-block"> |
| 11 | +<p><strong>Input:</strong> <span class="example-io">s = "00011"</span></p> |
| 12 | + |
| 13 | +<p><strong>Output:</strong> <span class="example-io">5</span></p> |
| 14 | + |
| 15 | +<p><strong>Explanation:</strong></p> |
| 16 | + |
| 17 | +<p>The substrings with dominant ones are shown in the table below.</p> |
| 18 | +</div> |
| 19 | + |
| 20 | +<table> |
| 21 | + <thead> |
| 22 | + <tr> |
| 23 | + <th>i</th> |
| 24 | + <th>j</th> |
| 25 | + <th>s[i..j]</th> |
| 26 | + <th>Number of Zeros</th> |
| 27 | + <th>Number of Ones</th> |
| 28 | + </tr> |
| 29 | + </thead> |
| 30 | + <tbody> |
| 31 | + <tr> |
| 32 | + <td>3</td> |
| 33 | + <td>3</td> |
| 34 | + <td>1</td> |
| 35 | + <td>0</td> |
| 36 | + <td>1</td> |
| 37 | + </tr> |
| 38 | + <tr> |
| 39 | + <td>4</td> |
| 40 | + <td>4</td> |
| 41 | + <td>1</td> |
| 42 | + <td>0</td> |
| 43 | + <td>1</td> |
| 44 | + </tr> |
| 45 | + <tr> |
| 46 | + <td>2</td> |
| 47 | + <td>3</td> |
| 48 | + <td>01</td> |
| 49 | + <td>1</td> |
| 50 | + <td>1</td> |
| 51 | + </tr> |
| 52 | + <tr> |
| 53 | + <td>3</td> |
| 54 | + <td>4</td> |
| 55 | + <td>11</td> |
| 56 | + <td>0</td> |
| 57 | + <td>2</td> |
| 58 | + </tr> |
| 59 | + <tr> |
| 60 | + <td>2</td> |
| 61 | + <td>4</td> |
| 62 | + <td>011</td> |
| 63 | + <td>1</td> |
| 64 | + <td>2</td> |
| 65 | + </tr> |
| 66 | + </tbody> |
| 67 | +</table> |
| 68 | + |
| 69 | +<p><strong class="example">Example 2:</strong></p> |
| 70 | + |
| 71 | +<div class="example-block"> |
| 72 | +<p><strong>Input:</strong> <span class="example-io">s = "101101"</span></p> |
| 73 | + |
| 74 | +<p><strong>Output:</strong> <span class="example-io">16</span></p> |
| 75 | + |
| 76 | +<p><strong>Explanation:</strong></p> |
| 77 | + |
| 78 | +<p>The substrings with <strong>non-dominant</strong> ones are shown in the table below.</p> |
| 79 | + |
| 80 | +<p>Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.</p> |
| 81 | +</div> |
| 82 | + |
| 83 | +<table> |
| 84 | + <thead> |
| 85 | + <tr> |
| 86 | + <th>i</th> |
| 87 | + <th>j</th> |
| 88 | + <th>s[i..j]</th> |
| 89 | + <th>Number of Zeros</th> |
| 90 | + <th>Number of Ones</th> |
| 91 | + </tr> |
| 92 | + </thead> |
| 93 | + <tbody> |
| 94 | + <tr> |
| 95 | + <td>1</td> |
| 96 | + <td>1</td> |
| 97 | + <td>0</td> |
| 98 | + <td>1</td> |
| 99 | + <td>0</td> |
| 100 | + </tr> |
| 101 | + <tr> |
| 102 | + <td>4</td> |
| 103 | + <td>4</td> |
| 104 | + <td>0</td> |
| 105 | + <td>1</td> |
| 106 | + <td>0</td> |
| 107 | + </tr> |
| 108 | + <tr> |
| 109 | + <td>1</td> |
| 110 | + <td>4</td> |
| 111 | + <td>0110</td> |
| 112 | + <td>2</td> |
| 113 | + <td>2</td> |
| 114 | + </tr> |
| 115 | + <tr> |
| 116 | + <td>0</td> |
| 117 | + <td>4</td> |
| 118 | + <td>10110</td> |
| 119 | + <td>2</td> |
| 120 | + <td>3</td> |
| 121 | + </tr> |
| 122 | + <tr> |
| 123 | + <td>1</td> |
| 124 | + <td>5</td> |
| 125 | + <td>01101</td> |
| 126 | + <td>2</td> |
| 127 | + <td>3</td> |
| 128 | + </tr> |
| 129 | + </tbody> |
| 130 | +</table> |
| 131 | + |
| 132 | +<p> </p> |
| 133 | +<p><strong>Constraints:</strong></p> |
| 134 | + |
| 135 | +<ul> |
| 136 | + <li><code>1 <= s.length <= 4 * 10<sup>4</sup></code></li> |
| 137 | + <li><code>s</code> consists only of characters <code>'0'</code> and <code>'1'</code>.</li> |
| 138 | +</ul> |
0 commit comments