Question 1 — N boxes are Arrranged in a line. Each box is colored either with white or black. You can select any number of boxes from the starting and put it behind the last box in the same sequence as it was placed initially. you can perform this operation atmost once. For example string s="wbbb" . In one operation you can select "wb" and from the start and put it behind the last box "b".
Note: you cannot select all the n boxes.
You are required to find the minimum number of boxes whose colors must be reversed so that all the boxes in the sequence are of an alternate color, that is any two adjacent boxes are of different colors.
Input : 5
wwwwwbbww
wwwb
bwww
wwww
bbbbb
Output :
3
1
1
2
2
I was thinking of brute forcing the solution, that is doing operation for each length and then calculating for that configuration. Can anyobody tell the correct approach
Append string to itself and use sliding window of size N , and calculate each window the transition required to convert, now while moving the window you could also manage the dp states , dp will be minimum operation required to convert current window that is ending at index i and such that ith character is W or B, and before that there is alternating sequence for the range [i-N+1,i].
Let
pref[i][0]
show the minimum number of flips to make the characters[0, i)
alternating while the first character itself beingW
.pref[i][1]
shows the same thing but the first character needs to be aB
there. Do this calculation for all suffix too, wheresuff[i][0]
shows the characters[i, n)
alternating while the last one isW
andsuff[i][1]
for the last one beingB
. Those calculations areO(n)
. Then the answer is minimum ofmin(pref[i][0] + suff[i+1][0] + 1, pref[i][1] + suff[i+1][0], pref[i][0] + suff[i+1][1], pref[i][1] + suff[i+1][1] + 1)
for alli
. This is alsoO(n)
.I want to ask you 2 things:-
pref[i]
contains[0,i)
whilesuff[i]
contains[i,n)
and in the end you're checking forpref[i]+suff[i+1]
, which means you're not consideringith
character at all.'W'
to an alternating suffix which ends with'W'
(for ex...BWBWBW + WBWBWB...
) then I believe the characters of at least one of the string should be flipped (i.e....BWBWBW + BWBWBW...
or...WBWBWB + WBWBWB...
) which may cost more than 1 depending on thelength
and values ofpref[i] and suff[i].
Please correct me If I'm wrong or If I missed something.
Correct, my bad.
Should be
pref[i]+suff[i]
Now I think about it, I believe you don't need to check for +1 cases (do you? I might look into it later).
I think we need to consider them. And the cost should depend on the relation between two numbers
X
andY
whereX is number of operations required to form an alternating string S
whileY is the number of operations required to from the counter alternating string of S
(Sorry couldn't find a better word) and If I'm not wrong, the relation should beY = len(S)-X.
it's very easy
1. pre-compute suffix array cost making wbwbwb and bwbwbw.
-> ans = min(ans ,prefix cost of starting with w and prefix end with b )
-> ans = min(ans ,prefix cost of starting with b and prefix end with w )
void solve(){
}