Find Binary string by converting all 01 or 10 to 11 after M iterations | Function to find the modified binary let after M iterations ; Set the value of M to the minimum of N or M . ; Declaration of current let 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 ; Given let ; Number of Iterations ; Function Call
function findlet ( str , M ) { let N = str . length ; M = Math . min ( M , N ) ; let s1 = " " ; while ( M != 0 ) { s1 = " " ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { if ( ( str [ i - 1 ] == ' ' && str [ i + 1 ] != ' ' ) || ( str [ i - 1 ] != ' ' && str [ i + 1 ] == ' ' ) ) s1 += ' ' ; else s1 += ' ' ; } else s1 += ' ' ; } if ( str == s1 ) break ; str = s1 ; M -- ; } document . write ( s1 ) ; } let str = " " ; let M = 3 ; findlet ( str , M ) ;