Minimum number of flipping adjacent bits required to make given Binary Strings equal | Java program for the above approach ; Function to find the minimum number of inversions required . ; Initializing the answer ; Iterate over the range ; If s1 [ i ] != s2 [ i ] , then inverse the characters at i snd ( i + 1 ) positions in s1 . ; Adding 1 to counter if characters are not same ; Driver Code
import java . util . * ; class GFG { static int find_Min_Inversion ( int n , char [ ] s1 , char [ ] s2 ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { if ( s1 [ i ] == '1' ) { s1 [ i ] = '0' ; } else { s1 [ i ] = '1' ; } if ( s1 [ i + 1 ] == '1' ) { s1 [ i + 1 ] = '0' ; } else { s1 [ i + 1 ] = '1' ; } count ++ ; } } if ( String . copyValueOf ( s1 ) . equals ( String . copyValueOf ( s2 ) ) ) { return count ; } return - 1 ; } public static void main ( String [ ] args ) { int n = 4 ; String s1 = "0101" ; String s2 = "1111" ; System . out . print ( find_Min_Inversion ( n , s1 . toCharArray ( ) , s2 . toCharArray ( ) ) + "NEW_LINE"); } }