📌  相关文章
📜  使两个字符串相等所需的相邻字符的最少翻转或交换次数

📅  最后修改于: 2021-09-22 09:48:20             🧑  作者: Mango

给定两个长度为N 的二进制字符串AB ,任务是通过交换相邻字符或翻转字符串A 的任何字符来计算使两个给定字符串相等所需的最小操作次数。

例子:

方法:该问题可以使用动态规划解决,因为它具有重叠子问题和最优子结构。
请按照以下步骤解决问题:

  • 初始化一个数组,比如大小为N+1 的dp[]{0} ,其中dp[i]存储索引i所需的最小操作数以使A i的前缀等于前缀B i
  • 使用变量(例如i )迭代范围[1, N]并执行以下操作:
    • 如果A[i – 1]等于B[i – 1],则将dp[i]更新为dp[i – 1]
    • 否则,将dp[i]更新为dp[i – 1] + 1
    • 如果可以交换,即i > 1A[i – 2]等于B[i – 1]A[i – 1]等于B[i – 2],则将dp[i]更新为min (dp[i], dp[i – 2] + 1)。
  • 完成以上步骤后,打印得到的最小操作次数,即值dp[N]。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the minimum
// number of operations required
// to make strings A and B equal
int countMinSteps(string A, string B, int N)
{
 
    // Stores all dp-states
    vector dp(N + 1, 0);
 
    // Iterate over the range [1, N]
    for (int i = 1; i <= N; i++) {
 
        // If A[i - 1] equals to B[i - 1]
        if (A[i - 1] == B[i - 1]) {
 
            // Assign Dp[i - 1] to Dp[i]
            dp[i] = dp[i - 1];
        }
 
        // Otherwise
        else {
 
            // Update dp[i]
            dp[i] = dp[i - 1] + 1;
        }
 
        // If swapping is possible
        if (i >= 2 && A[i - 2] == B[i - 1]
            && A[i - 1] == B[i - 2]) {
 
            // Update dp[i]
            dp[i] = min(dp[i], dp[i - 2] + 1);
        }
    }
 
    // Retun the minimum
    // number of steps required
    return dp[N];
}
 
// Driver Code
int main()
{
    // Given Input
    string A = "0101";
    string B = "0011";
    int N = A.length();
 
    // Function Call
    cout << countMinSteps(A, B, N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to count the minimum
// number of operations required
// to make strings A and B equal
static int countMinSteps(String A, String B, int N)
{
     
    // Stores all dp-states
    int[] dp = new int[N + 1];
    for(int i = 1; i <= N; i++)
    {
         
        // Update the value of A[i]
        dp[i] = 0;
    }
 
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
         
        // If A[i - 1] equals to B[i - 1]
        if (A.charAt(i - 1) == B.charAt(i - 1))
        {
             
            // Assign Dp[i - 1] to Dp[i]
            dp[i] = dp[i - 1];
        }
 
        // Otherwise
        else
        {
             
            // Update dp[i]
            dp[i] = dp[i - 1] + 1;
        }
 
        // If swapping is possible
        if (i >= 2 && A.charAt(i - 2) == B.charAt(i - 1) &&
                      A.charAt(i - 1) == B.charAt(i - 2))
        {
             
            // Update dp[i]
            dp[i] = Math.min(dp[i], dp[i - 2] + 1);
        }
    }
 
    // Retun the minimum
    // number of steps required
    return dp[N];
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Input
    String A = "0101";
    String B = "0011";
    int N = A.length();
     
    // Function Call
    System.out.println(countMinSteps(A, B, N));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to count the minimum
# number of operations required
# to make strings A and B equal
def countMinSteps(A, B, N) :
 
    # Stores all dp-states
    dp = [0] * (N + 1)
 
    # Iterate rate over the range [1, N]
    for i in range(1, N+1) :
 
        # If A[i - 1] equals to B[i - 1]
        if (A[i - 1] == B[i - 1]) :
 
            # Assign Dp[i - 1] to Dp[i]
            dp[i] = dp[i - 1]
         
 
        # Otherwise
        else :
 
            # Update dp[i]
            dp[i] = dp[i - 1] + 1
         
 
        # If swapping is possible
        if (i >= 2 and A[i - 2] == B[i - 1]
            and A[i - 1] == B[i - 2]) :
 
            # Update dp[i]
            dp[i] = min(dp[i], dp[i - 2] + 1)
         
    # Retun the minimum
    # number of steps required
    return dp[N]
 
 
# Driver Code
 
# Given Input
A = "0101"
B = "0011"
N = len(A)
 
# Function Call
print(countMinSteps(A, B, N))
 
# This code is contributed by splevel62.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the minimum
// number of operations required
// to make strings A and B equal
static int countMinSteps(string A, string B, int N)
{
     
    // Stores all dp-states
    int[] dp = new int[N + 1];
    for(int i = 1; i <= N; i++)
    {
         
        // Update the value of A[i]
        dp[i] = 0;
    }
 
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
         
        // If A[i - 1] equals to B[i - 1]
        if (A[i - 1] == B[i - 1])
        {
             
            // Assign Dp[i - 1] to Dp[i]
            dp[i] = dp[i - 1];
        }
 
        // Otherwise
        else
        {
             
            // Update dp[i]
            dp[i] = dp[i - 1] + 1;
        }
 
        // If swapping is possible
        if (i >= 2 && A[i - 2] == B[i - 1] &&
                      A[i - 1] == B[i - 2])
        {
             
            // Update dp[i]
            dp[i] = Math.Min(dp[i], dp[i - 2] + 1);
        }
    }
 
    // Retun the minimum
    // number of steps required
    return dp[N];
}
 
// Driver code
public static void Main(String []args)
{
     
    // Given Input
    string A = "0101";
    string B = "0011";
    int N = A.Length;
 
    // Function Call
    Console.Write(countMinSteps(A, B, N));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
1

时间复杂度: O(N)
辅助空间: O(N)

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