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

📅  最后修改于: 2021-10-26 06:51:42             🧑  作者: Mango

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

  1. 任何字符都可以从“a”变为“b”或从“b”变为“a” ,单位成本为1
  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


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程