Class Solution

java.lang.Object
g1501_1600.s1529_bulb_switcher_iv.Solution

public class Solution extends Object
1529 - Minimum Suffix Flips.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> binary string <code>target</code> of length <code>n</code>. You have another binary string <code>s</code> of length <code>n</code> that is initially set to all zeros. You want to make <code>s</code> equal to <code>target</code>.</p> <p>In one operation, you can pick an index <code>i</code> where <code>0 <= i < n</code> and flip all bits in the <strong>inclusive</strong> range <code>[i, n - 1]</code>. Flip means changing <code>'0'</code> to <code>'1'</code> and <code>'1'</code> to <code>'0'</code>.</p> <p>Return <em>the minimum number of operations needed to make</em> <code>s</code> <em>equal to</em> <code>target</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> target = &ldquo;10111&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> Initially, s = &ldquo;00000&rdquo;.</p> <p>Choose index i = 2: &ldquo;00000&rdquo; -> &ldquo;00111&rdquo;</p> <p>Choose index i = 0: &ldquo;00111&rdquo; -> &ldquo;11000&rdquo;</p> <p>Choose index i = 1: &ldquo;11000&rdquo; -> &ldquo;10111&rdquo;</p> <p>We need at least 3 flip operations to form target.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> target = &ldquo;101&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> Initially, s = &ldquo;000&rdquo;.</p> <p>Choose index i = 0: &ldquo;000&rdquo; -> &ldquo;111&rdquo;</p> <p>Choose index i = 1: &ldquo;111&rdquo; -> &ldquo;100&rdquo;</p> <p>Choose index i = 2: &ldquo;100&rdquo; -> &ldquo;101&rdquo;</p> <p>We need at least 3 flip operations to form target.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> target = &ldquo;00000&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> We do not need any operations since the initial s already equals target.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == target.length</code></li> <li><code>1 <= n <= 10<sup>5</sup></code></li> <li><code>target[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minFlips

      public int minFlips(String target)