Minimum flips or swapping of adjacent characters required to make a string equal to another | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumOperation ( string a , string b ) { int n = a . length ( ) ; int i = 0 ; int minoperation = 0 ; while ( i < n ) { if ( a [ i ] == b [ i ] ) { i = i + 1 ; continue ; } else if ( a [ i ] == b [ i + 1 ] && a [ i + 1 ] == b [ i ] && i < n - 1 ) { minoperation ++ ; i = i + 2 ; } else if ( a [ i ] != b [ i ] ) { minoperation ++ ; i = i + 1 ; } else { ++ i ; } } cout << minoperation ; } int main ( ) { string a = "10010010" , b = "00001000" ; minimumOperation ( a , b ) ; return 0 ; }