Problem: K-Step Bit Flip with Retention
You are given a binary string S of length n and a positive integer k. You must apply exactly k transformations to S, one after another. Think of the transformations as being labeled with the values k, k-1, k-2, ..., 1 (i.e., the first transformation applied is labeled k, the last is labeled 1).
For the transformation labeled with value v, applied to the current string:
If v is odd: find the leftmost '1' in the current string. This position is the “retained” position.
If v is even: find the leftmost '0' in the current string. This position is the “retained” position.
If the string has no character of the required type (e.g., it’s odd’s turn but the string is all '0's, or even’s turn but it’s all '1's), retain the last index of the string instead.
Then produce the new string by flipping every character ('0' → '1', '1' → '0') except the retained position, which stays unchanged.
Repeat this for all k labeled transformations (k down to 1), and return the final string.
Example
S = "1010"
k = 1 -> "1101"
k = 2 -> "1111"
k = 3 -> "1110"
k = 4 -> "1111"


