Minimum cost to convert str1 to str2 with the given operations | Java implementation of the approach ; Function to return the minimum cost to convert str1 to sr2 ; For every character of str1 ; If current character is not equal in both the strings ; If the next character is also different in both the strings then these characters can be swapped ; Change the current character ; Driver code
import java . util . * ; class GFG { static int minCost ( char [ ] str1 , char [ ] str2 , int n ) { int cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str1 [ i ] != str2 [ i ] ) { if ( i < n - 1 && str1 [ i + 1 ] != str2 [ i + 1 ] ) { swap ( str1 , i , i + 1 ) ; cost ++ ; } else { cost ++ ; } } } return cost ; } static void swap ( char [ ] arr , int i , int j ) { char temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } public static void main ( String [ ] args ) { String str1 = " abb " , str2 = " bba " ; int n = str1 . length ( ) ; System . out . println ( minCost ( str1 . toCharArray ( ) , str2 . toCharArray ( ) , n ) ) ; } }