Input: s ="111000"Output: 2Explanation: Use the first operation two times to make s ="100011".Then, use the second operation on the third and sixth elements to make s ="10 _1_ 01 _0_ ".
Lets take a string "111000". The target state can be S1 - "010101" OR S2 - "101010".
Now if we are allowed to just second operation i.e. Type 2, then number of flips to get to S1 OR S2 will be O(n). But we are also allowed operation Type 1, where we take prefix and append to the end of string.
Now the brute force can be we take input string, perform T2 operation, check updated string is S1 OR S2.
Method 2 - Sliding Window with Alternating Patterns#
Since we can rotate the string any number of times, we can try all possible rotations. For each rotation, we count the minimum flips needed to make the string alternating (either starting with ‘0’ or ‘1’). We use a sliding window to efficiently check all rotations.
For the 1st operation T1, we can simply do s += s to append the whole string to the end.
then we make two different string with the same length by 01 and 10 alternative. for example: s = 11100
s = 1110011100
s1= 1010101010
s2= 0101010101
finally, use sliding window(size n)to compare s to both s1 and s2.
why we can double s to fullfil the first operation, let’s look at the same example s = 11100:`
double s: 1110011100
size n window: |11100|11100 ==> 1|11001|1100 ==> 11|10011|100 and so on, when we move one step of the sliding window, it is the same to append 1 more element from beginning.
Here is the summary:
Concatenate the string to itself to simulate all rotations.
For each window of length n, count flips needed for both patterns (‘0101…’ and ‘1010…’).
classSolution {
public:int minFlips(string s) {
int n = s.size();
string ss = s + s;
int ans = n;
int flip0 =0, flip1 =0;
for (int i =0; i <2*n; ++i) {
char c = ss[i];
if (c != (i%2?'1':'0')) flip0++;
if (c != (i%2?'0':'1')) flip1++;
if (i >= n) {
char out = ss[i-n];
if (out != ((i-n)%2?'1':'0')) flip0--;
if (out != ((i-n)%2?'0':'1')) flip1--;
}
if (i >= n-1) ans = min(ans, min(flip0, flip1));
}
return ans;
}
};
classSolution {
publicintminFlips(String s) {
int n = s.length();
String ss = s + s;
int ans = n, flip0 = 0, flip1 = 0;
for (int i = 0; i < 2*n; ++i) {
char c = ss.charAt(i);
if (c != (i%2==0 ?'0' : '1')) flip0++;
if (c != (i%2==0 ?'1' : '0')) flip1++;
if (i >= n) {
char out = ss.charAt(i-n);
if (out != ((i-n)%2==0 ?'0' : '1')) flip0--;
if (out != ((i-n)%2==0 ?'1' : '0')) flip1--;
}
if (i >= n-1) ans = Math.min(ans, Math.min(flip0, flip1));
}
return ans;
}
}
classSolution {
funminFlips(s: String): Int {
val n = s.length
val ss = s + s
var ans = n
var flip0 = 0var flip1 = 0for (i in0 until 2*n) {
val c = ss[i]
if (c !=if (i%2==0) '0'else'1') flip0++if (c !=if (i%2==0) '1'else'0') flip1++if (i >= n) {
val out = ss[i-n]
if (out!=if ((i-n)%2==0) '0'else'1') flip0--if (out!=if ((i-n)%2==0) '1'else'0') flip1-- }
if (i >= n-1) ans = minOf(ans, minOf(flip0, flip1))
}
return ans
}
}
classSolution:
defminFlips(self, s: str) -> int:
n: int = len(s)
ss: str = s + s
ans: int = n
flip0: int =0 flip1: int =0for i in range(2*n):
c = ss[i]
if c != ('0'if i%2==0else'1'):
flip0 +=1if c != ('1'if i%2==0else'0'):
flip1 +=1if i >= n:
out = ss[i-n]
if out != ('0'if (i-n)%2==0else'1'):
flip0 -=1if out != ('1'if (i-n)%2==0else'0'):
flip1 -=1if i >= n-1:
ans = min(ans, flip0, flip1)
return ans
impl Solution {
pubfnmin_flips(s: String) -> i32 {
let n = s.len();
let ss = s.clone() +&s;
letmut ans = n asi32;
letmut flip0 =0;
letmut flip1 =0;
let bytes = ss.as_bytes();
for i in0..2*n {
let c = bytes[i];
if c !=if i%2==0 { b'0' } else { b'1' } { flip0 +=1; }
if c !=if i%2==0 { b'1' } else { b'0' } { flip1 +=1; }
if i >= n {
let out = bytes[i-n];
if out !=if (i-n)%2==0 { b'0' } else { b'1' } { flip0 -=1; }
if out !=if (i-n)%2==0 { b'1' } else { b'0' } { flip1 -=1; }
}
if i >= n-1 {
ans = ans.min(flip0).min(flip1);
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
minFlips(s: string):number {
constn=s.length;
constss=s+s;
letans=n, flip0=0, flip1=0;
for (leti=0; i<2*n; ++i) {
constc=ss[i];
if (c!== (i%2===0?'0':'1')) flip0++;
if (c!== (i%2===0?'1':'0')) flip1++;
if (i>=n) {
constout=ss[i-n];
if (out!== ((i-n)%2===0?'0':'1')) flip0--;
if (out!== ((i-n)%2===0?'1':'0')) flip1--;
}
if (i>=n-1) ans= Math.min(ans, flip0, flip1);
}
returnans;
}
}