Minimum cost to convert str1 to str2 with the given operations | 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
function minCost ( str1 , str2 , n ) { let cost = 0 ; let array = str1 . split ( ' ' ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( str1 [ i ] != str2 [ i ] ) { if ( i < n - 1 && str1 [ i + 1 ] != str2 [ i + 1 ] ) { let temp = array [ i ] ; array [ i ] = array [ i + 1 ] ; array [ i + 1 ] = temp ; cost ++ ; } else { cost ++ ; } } } return cost ; } let str1 = " " , str2 = " " ; let n = str1 . length ; document . write ( minCost ( str1 , str2 , n ) ) ;