write a program where two integers in N K and a string S of size n consisting of 0's and 1's determine whether it is possible to convert every character of string S to '0', by choosing a substring of size K and change each '0' into '1' and vice versa
Answers
Answered by
1
Answer:
We can solve this problem by considering all possible results, As we are supposed to get alternate string, there are only 2 possibilities, alternate string starting with 0 and alternate string starting with 1. We will try both cases and choose the string which will require minimum number of flips as our final answer.
Trying a case requires O(n) time in which we will loop over all characters of given string, if current character is expected character according to alternation then we will do nothing otherwise we will increase flip count by 1. After trying strings starting with 0 and starting with 1, we will choose the string with minimum flip count.
Total time complexity of solution will be O(n)
Similar questions