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
function countWays ( s1 , s2 , n ) { var a , b , c , d ; a = b = c = d = 0 ; var result = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s2 . charAt ( i ) == ' ' ) { if ( s1 . charAt ( i ) == ' ' ) { c ++ ; } else { d ++ ; } } else { if ( s1 . charAt ( i ) == ' ' ) { a ++ ; } else { b ++ ; } } } result = a * d + b * c + c * d ; return result ; } var n = 5 ; var s1 = " " ; var s2 = " " ; document . write ( countWays ( s1 , s2 , n ) ) ;