Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes | Java program to find no of ways to swap bits 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
import java . io . * ; class GFG { static int countWays ( String s1 , String s2 , int n ) { int a , b , c , d ; a = b = c = d = 0 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s2 . charAt ( i ) == '0' ) { if ( s1 . charAt ( i ) == '0' ) { c ++ ; } else { d ++ ; } } else { if ( s1 . charAt ( i ) == '0' ) { a ++ ; } else { b ++ ; } } } result = a * d + b * c + c * d ; return result ; } public static void main ( String [ ] args ) { int n = 5 ; String s1 = "01011" ; String s2 = "11001" ; System . out . println ( countWays ( s1 , s2 , n ) ) ; } }