Solved! Leetcode 1529. Minimum Suffix Flips

Description: Minimum Suffix Flips

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.

Example 1

Example 2

Example 3

Constraints

  • n == target.length
  • 1 <= n <= 105
  • target[i] is either '0' or '1'.

Solution

Time Complexity

O(n), where n is the number of characters in a string

Space Complexity

O(1)

Rate this post

Leave a Reply