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
Input
s = "110", k = 1Output
1There is one
'0' in s, and since k = 1, it can be flipped directly in one operation.Example 2
Input
s = "0101", k = 3Output
2One 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