Minimum Operations to Equalize Binary String

You are given a binary string s, and an integer k.

In one operation, you must choose exactly k different indices and flip each '0' to '1' and each '1' to '0'.

Return the minimum number of operations required to make all characters in the string equal to '1'. If it is not possible, return -1.

Example 1
Inputs = "110", k = 1
Output1
There is one '0' in s, and since k = 1, it can be flipped directly in one operation.
Example 2
Inputs = "0101", k = 3
Output2
One optimal sequence flips indices [0, 1, 3] to change "0101" to "1000", then flips [1, 2, 3] to make "1111", for a minimum of 2 operations.

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is either '0' or '1'.
  • 1 <= k <= s.length

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate