Minimum steps to convert one binary string to other only using negation | Function to find the minimum steps to convert string a to string b ; array to mark the positions needed to be negated ; If two character are not same then they need to be negated ; To count the blocks of 1 ; To count the number of 1 ' s ▁ in ▁ ▁ each ▁ block ▁ of ▁ 1' s ; For the last block of 1 's ; Driver code
< ? php function convert ( $ n , $ a , $ b ) { $ l = array_fill ( 0 , $ n , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ l [ $ i ] = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] != $ b [ $ i ] ) $ l [ $ i ] = 1 ; } $ cc = 0 ; $ vl = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ l [ $ i ] == 0 ) { if ( $ vl != 0 ) $ cc += 1 ; $ vl = 0 ; } else $ vl += 1 ; } if ( $ vl != 0 ) $ cc += 1 ; echo $ cc . " STRNEWLINE " ; } $ a = "101010" ; $ b = "110011" ; $ n = strlen ( $ a ) ; convert ( $ n , $ a , $ b ) ; ? >