Find Binary string by converting all 01 or 10 to 11 after M iterations | Function to find the modified binary string after M iterations ; Set the value of M to the minimum of N or M . ; Declaration of current string state ; Loop over M iterations ; Set the current state as null before each iteration ; Check if this zero has exactly one 1 as neighbour ; Flip the zero ; If there is no change , then no need for further iterations . ; Set the current state as the new previous state ; Driver Code ; Given String ; Number of Iterations ; Function Call
def findString ( str , M ) : NEW_LINE INDENT N = len ( str ) NEW_LINE M = min ( M , N ) NEW_LINE s1 = " " NEW_LINE while ( M != 0 ) : NEW_LINE INDENT s1 = " " NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT if ( ( str [ i - 1 ] == '1' and str [ i + 1 ] != '1' ) or ( str [ i - 1 ] != '1' and str [ i + 1 ] == '1' ) ) : NEW_LINE INDENT s1 += '1' NEW_LINE DEDENT else : NEW_LINE INDENT s1 += '0' NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT s1 += '1' NEW_LINE DEDENT DEDENT if ( str == s1 ) : NEW_LINE INDENT break NEW_LINE DEDENT s1 += '1' NEW_LINE str = s1 NEW_LINE M -= 1 NEW_LINE DEDENT print ( s1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "0110100" NEW_LINE M = 3 NEW_LINE findString ( str , M ) NEW_LINE DEDENT