Minimum prefixes required to be flipped to convert a Binary String to another | Java program for the above approach ; Function to count flips required to make Strings A and B equal ; Stores the count of the number of operations ; Stores the length of the chosen prefixes ; Stores if operations are performed even or odd times ; Traverse the given String ; If current characters in the two Strings are unequal ; If A [ i ] is not flipped ; Increment count of operations ; Insert the length of the chosen prefix ; Update invert to true ; If A [ i ] is flipped ; Increment count of operations ; Insert length of the chosen prefix ; Update invert to false ; Print the number of operations required ; Print the chosen prefix length in each operation ; Driver Code ; Given binary Strings
import java . util . * ; class GFG { static void findOperations ( String A , String B , int N ) { int operations = 0 ; Vector < Integer > ops = new Vector < > ( ) ; boolean invert = false ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A . charAt ( i ) != B . charAt ( i ) ) { if ( ! invert ) { operations ++ ; ops . add ( i + 1 ) ; invert = true ; } } else { if ( invert ) { operations ++ ; ops . add ( i + 1 ) ; invert = false ; } } } System . out . print ( operations + "NEW_LINE"); if ( operations != 0 ) { for ( int x : ops ) System . out . print ( x + " ▁ " ) ; } } public static void main ( String [ ] args ) { String A = "001" , B = "000" ; int N = A . length ( ) ; findOperations ( A , B , N ) ; } }