|
| 1 | +<h2><a href="https://leetcode.com/problems/maximum-number-of-operations-to-move-ones-to-the-end">3493. Maximum Number of Operations to Move Ones to the End</a></h2><h3>Medium</h3><hr><p>You are given a <span data-keyword="binary-string">binary string</span> <code>s</code>.</p> |
| 2 | + |
| 3 | +<p>You can perform the following operation on the string <strong>any</strong> number of times:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Choose <strong>any</strong> index <code>i</code> from the string where <code>i + 1 < s.length</code> such that <code>s[i] == '1'</code> and <code>s[i + 1] == '0'</code>.</li> |
| 7 | + <li>Move the character <code>s[i]</code> to the <strong>right</strong> until it reaches the end of the string or another <code>'1'</code>. For example, for <code>s = "010010"</code>, if we choose <code>i = 1</code>, the resulting string will be <code>s = "0<strong><u>001</u></strong>10"</code>.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>Return the <strong>maximum</strong> number of operations that you can perform.</p> |
| 11 | + |
| 12 | +<p> </p> |
| 13 | +<p><strong class="example">Example 1:</strong></p> |
| 14 | + |
| 15 | +<div class="example-block"> |
| 16 | +<p><strong>Input:</strong> <span class="example-io">s = "1001101"</span></p> |
| 17 | + |
| 18 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 19 | + |
| 20 | +<p><strong>Explanation:</strong></p> |
| 21 | + |
| 22 | +<p>We can perform the following operations:</p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li>Choose index <code>i = 0</code>. The resulting string is <code>s = "<u><strong>001</strong></u>1101"</code>.</li> |
| 26 | + <li>Choose index <code>i = 4</code>. The resulting string is <code>s = "0011<u><strong>01</strong></u>1"</code>.</li> |
| 27 | + <li>Choose index <code>i = 3</code>. The resulting string is <code>s = "001<strong><u>01</u></strong>11"</code>.</li> |
| 28 | + <li>Choose index <code>i = 2</code>. The resulting string is <code>s = "00<strong><u>01</u></strong>111"</code>.</li> |
| 29 | +</ul> |
| 30 | +</div> |
| 31 | + |
| 32 | +<p><strong class="example">Example 2:</strong></p> |
| 33 | + |
| 34 | +<div class="example-block"> |
| 35 | +<p><strong>Input:</strong> <span class="example-io">s = "00111"</span></p> |
| 36 | + |
| 37 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 38 | +</div> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | +<p><strong>Constraints:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>1 <= s.length <= 10<sup>5</sup></code></li> |
| 45 | + <li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li> |
| 46 | +</ul> |
0 commit comments