Convert a Binary String to another by flipping prefixes minimum number of times | Function to count minimum number of operations required to convert string a to another string b ; Store the lengths of each prefixes selected ; Traverse the string ; If first character is same as b [ i ] ; Insert 1 to ops [ ] ; And , flip the bit ; Reverse the prefix string of length i + 1 ; Flip the characters in this prefix length ; Push ( i + 1 ) to array ops [ ] ; Print the number of operations ; Print the length of each prefixes stored ; Function to reverse a [ ] from start to end ; Driver Code
function minOperations ( s1 , s2 , n ) { var a = s1 . split ( " " ) ; var b = s2 . split ( " " ) ; var ops = [ ] ; for ( var i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] !== b [ i ] ) { if ( a [ 0 ] === b [ i ] ) { ops . push ( 1 ) ; a [ 0 ] = a [ 0 ] === " " ? " " : " " ; } reverse ( a , 0 , i ) ; for ( var j = 0 ; j <= i ; j ++ ) { a [ j ] = a [ j ] === " " ? " " : " " ; } ops . push ( i + 1 ) ; } } document . write ( ops . length + " " ) ; for ( const x of ops ) { document . write ( x + " " ) ; } } function reverse ( a , start , end ) { while ( start < end ) { var temp = a [ start ] ; a [ start ] = a [ end ] ; a [ end ] = temp ; start ++ ; end -- ; } } var a = " " , b = " " ; var N = a . length ; minOperations ( a , b , N ) ;