Minimum prefixes required to be flipped to convert a Binary String to another | C # 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
using System ; using System . Collections . Generic ; class GFG { static void findOperations ( String A , String B , int N ) { int operations = 0 ; List < int > ops = new List < int > ( ) ; bool invert = false ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != B [ i ] ) { if ( ! invert ) { operations ++ ; ops . Add ( i + 1 ) ; invert = true ; } } else { if ( invert ) { operations ++ ; ops . Add ( i + 1 ) ; invert = false ; } } } Console . Write ( operations + " STRNEWLINE " ) ; if ( operations != 0 ) { foreach ( int x in ops ) Console . Write ( x + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String A = "001" , B = "000" ; int N = A . Length ; findOperations ( A , B , N ) ; } }