📌  相关文章
📜  通过给定操作将str1转换为str2的最低成本

📅  最后修改于: 2021-04-28 00:15:38             🧑  作者: Mango

给定两个长度相等的字符串str1str2,仅由字符‘a’‘b’组成。可以在str1上执行以下操作:

  1. 任何字符都可以用1个单位的费用从‘a’更改为‘b’或从‘b’更改为‘a’
  2. 任何两个字符str1 [i]str1 [j]都可以与成本| i – j |交换

任务是找到将str1转换为str2所需的最低成本。

例子:

方法:可以观察到交换将仅在连续的字符上执行,因为如果字符不是连续的,则交换的成本将≥2,这将使成本大于或等于使用命令操作更改这些字符的成本。第一种。现在,对于每两个连续的字符,如果两个字符串的字符都不相同,则交换这些字符产生1单位成本,而当两个字符分别更改时,则产生2单位成本。否则,更改两个字符串(当前字符串或下一个字符串)不同的字符。最后,打印计算出的成本。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum
// cost to convert str1 to sr2
int minCost(string str1, string str2, int n)
{
    int cost = 0;
  
    // For every character of str1
    for (int i = 0; i < n; i++) {
  
        // If current character is not
        // equal in both the strings
        if (str1[i] != str2[i]) {
  
            // If the next character is also different in both
            // the strings then these characters can be swapped
            if (i < n - 1 && str1[i + 1] != str2[i + 1]) {
                swap(str1[i], str1[i + 1]);
                cost++;
            }
  
            // Change the current character
            else {
                cost++;
            }
        }
    }
    return cost;
}
  
// Driver code
int main()
{
    string str1 = "abb", str2 = "bba";
    int n = str1.length();
  
    cout << minCost(str1, str2, n);
  
    return 0;
}


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


Python3
# Python3 implementation of the approach 
  
# Function to return the minimum 
# cost to convert str1 to sr2 
def minCost(str1, str2, n): 
  
    cost = 0
  
    # For every character of str1
    for i in range(n): 
  
        # If current character is not 
        # equal in both the strings 
        if (str1[i] != str2[i]): 
  
            # If the next character is also different in both 
            # the strings then these characters can be swapped 
            if (i < n - 1 and str1[i + 1] != str2[i + 1]): 
                swap(str1[i], str1[i + 1]) 
                cost += 1
              
            # Change the current character 
            else: 
                cost += 1
              
    return cost 
  
# Driver code 
if __name__ == '__main__': 
  
    str1 = "abb"
    str2 = "bba"
    n = len(str1) 
  
    print(minCost(str1, str2, n)) 
  
# This code is contributed by ashutosh450


C#
// C# implementation of the approach 
using System;
  
class GFG
{
      
    // Function to return the minimum 
    // cost to convert str1 to sr2 
    static int minCost(string str1, 
                       string str2, int n) 
    { 
        int cost = 0; 
      
        char[] array = str1.ToCharArray();
          
        // For every character of str1 
        for (int i = 0; i < n; i++) 
        { 
      
            // If current character is not 
            // equal in both the strings 
            if (str1[i] != str2[i]) 
            { 
      
                // If the next character is also different in both 
                // the strings then these characters can be swapped 
                if (i < n - 1 && str1[i + 1] != str2[i + 1])
                { 
                    char temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp ;
                      
                    cost++; 
                } 
      
                // Change the current character 
                else
                { 
                    cost++; 
                } 
            } 
        } 
        return cost; 
    } 
      
    // Driver code 
    static public void Main () 
    { 
        string str1 = "abb", str2 = "bba"; 
        int n = str1.Length; 
      
        Console.WriteLine(minCost(str1, str2, n)); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
2