GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #1529 - Minimum Suffix Flips - LeetCode JavaScript Solutions

Back to all solutions

#1529 - Minimum Suffix Flips

Medium • View on LeetCode • View on GitHub
String Greedy

Problem Description

You are given a 0-indexed binary string target of length n. You have another binary string s of length n that is initially set to all zeros. You want to make s equal to target.

In one operation, you can pick an index i where 0 <= i < n and flip all bits in the inclusive range [i, n - 1]. Flip means changing '0' to '1' and '1' to '0'.

Return the minimum number of operations needed to make s equal to target.

Solution

/**
 * @param {string} target
 * @return {number}
 */
var minFlips = function(target) {
  let current = '0';
  let result = 0;

  for (const bit of target) {
    if (bit !== current) {
      result++;
      current = bit;
    }
  }

  return result;
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.