Minimum flips or swapping of adjacent characters required to make a string equal to another | Java program for the above approach ; Function to find minimum operations required to convert string A to B ; Store the size of the string ; Store the required result ; Traverse the string , a ; If a [ i ] is equal to b [ i ] ; Check if swapping adjacent characters make the same - indexed characters equal or not ; Otherwise , flip the current bit ; Print the minimum number of operations ; Driver Code ; Given Input ; Function Call
public class GFG { static void minimumOperation ( String a , String b ) { int n = a . length ( ) ; int i = 0 ; int minoperation = 0 ; while ( i < n ) { if ( a . charAt ( i ) == b . charAt ( i ) ) { i = i + 1 ; continue ; } else if ( a . charAt ( i ) == b . charAt ( i + 1 ) && a . charAt ( i + 1 ) == b . charAt ( i ) && i < n - 1 ) { minoperation ++ ; i = i + 2 ; } else if ( a . charAt ( i ) != b . charAt ( i ) ) { minoperation ++ ; i = i + 1 ; } else { ++ i ; } } System . out . println ( minoperation ) ; } public static void main ( String [ ] args ) { String a = "10010010" , b = "00001000" ; minimumOperation ( a , b ) ; } }