Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes | Function to find number of ways ; initialise result that store No . of swaps required ; Traverse both strings and check the bits as explained ; calculate result ; Driver code
< ? php function countWays ( $ s1 , $ s2 , $ n ) { $ a = $ b = $ c = $ d = 0 ; $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s2 [ $ i ] == '0' ) { if ( $ s1 [ $ i ] == '0' ) { $ c ++ ; } else { $ d ++ ; } } else { if ( $ s1 [ $ i ] == '0' ) { $ a ++ ; } else { $ b ++ ; } } } $ result = $ a * $ d + $ b * $ c + $ c * $ d ; return $ result ; } $ n = 5 ; $ s1 = "01011" ; $ s2 = "11001" ; echo countWays ( $ s1 , $ s2 , $ n ) ; ? >