
Input: ‘N’ = 5, ‘K’ = 1, ‘STR’ = “10101”
Output: 01110
In this case, the indices satisfying the condition of ‘STR[i-1] = STR[i+1]’ are ‘1’, ‘2’, and ‘3’. Hence the string ‘STR’ after applying the given operation for ‘K(=1)’ times will be “01110”.
The first line will contain the integer ‘T’, the number of test cases.
Each test case consists of two lines.
The first line of input contains three integers, ‘N’ and ‘K’, denoting the length of the given string ‘STR’ and the number of times the given operation is to be performed.
Followed by a line containing a string of length ‘N’ denoting the string ‘STR’, consisting of only ‘0’ and ‘1’.
For each test case, print the final string after applying ‘K’ operations.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10
0 <= ‘K’ <= 10^9
It is guaranteed that the string ‘STR’ only consists of the character ‘0’ and ‘1’.
Time Limit: 1 sec
The idea for this approach is to apply the given operations one by one by creating a separate transformed string from the original string and replacing it at the end of the iteration.
We will do the above-mentioned approach ‘K’ times to get the final string ‘STR’.
Algorithm:
// The function will return the final string ‘STR’ after applying the given operation ‘K’ times.
String finalString(N, K, STR)
The main idea behind this approach is to find a cycle such that after applying some number of operations the string ‘STR’ returns to a state which has already been iterated.
If we find this cycle length, we don’t need to apply the operation ‘K’ times, we can easily find the remainder of ‘K’ to the length of the cycle (number of operations after which we again get the same string) and determine the final string. Because if there is a cycle of length ‘X’ then we can say that after ‘X’, ‘2*X’, ‘3*X’, … until <= ‘K’, the string ‘STR’ will remain the same.
Now we can also prove that this cycle length can at most be ‘2(N-2)’ using the pigeonhole principle. Because there can be at most ‘2N’ possible arrangements of ‘0’ and ‘1’ in string ‘STR’ after performing any number of operations, thus it is guaranteed that after at most ‘2N’ operations we will again reach the same string ‘STR’. And if we observe more accurately the first and last characters always become ‘0’ after applying the operations once. Thus the possible arrangements reduce to ‘2(N-2)’.
So in case ‘K < X’ then we can find the final string by brute force otherwise we can find the final string using above mentioned approach. We can use a HashMap to store the different states of string ‘STR’ we got after applying operations.
This solution is better than the O ( K * N ) brute force approach because for huge ‘K’ this solution reduces the operations to be performed because it keeps track of the previously visited strings and detects multiple cycles involved in the transformation process thus reducing the re-computation.
Algorithm:
// The function will return the final string ‘STR’ after applying the given operation ‘K’ times.
String finalString(N, K, STR)