📌  相关文章
📜  使一个字符串等于另一个字符串所需的相邻字符的最少翻转或交换

📅  最后修改于: 2021-10-26 05:43:31             🧑  作者: Mango

给定两个长度为N 的二进制字符串AB ,任务是通过翻转A 的任何字符或交换A最小次数的相邻字符来将字符串A转换为B。如果不可能使两个字符串相等,则打印-1

例子:

做法:思路是遍历字符串A ,通过先检查相邻字符的交换条件,尝试使相同索引的字符相等。如果此操作无法使字符相等,则翻转字符。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans,以存储所需的结果。
  • 使用变量遍历字符串A ,比如i ,并执行以下操作:
    • 如果A[i]等于B[i],则继续循环中的下一次迭代。
    • 否则,如果A[i]等于B[i + 1]并且A[i + 1]等于B[i] ,则交换字符并将ians增加1
    • 否则,如果A[i]不等于B[i] ,则翻转当前位并将ans增加1
  • 打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum operations
// required to convert string A to B
int minimumOperation(string a, string b)
{
 
    // Store the size of the string
    int n = a.length();
    int i = 0;
 
    // Store the required result
    int minoperation = 0;
 
    // Traverse the string, a
    while (i < n) {
 
        // If a[i] is equal to b[i]
        if (a[i] == b[i]) {
            i = i + 1;
            continue;
        }
 
        // Check if swapping adjacent
        // characters make the same-indexed
        // characters equal or not
        else if (a[i] == b[i + 1]
                 && a[i + 1] == b[i]
                 && i < n - 1) {
 
            minoperation++;
            i = i + 2;
        }
 
        // Otherwise, flip the current bit
        else if (a[i] != b[i]) {
            minoperation++;
            i = i + 1;
        }
        else {
            ++i;
        }
    }
 
    // Print the minimum number of operations
    cout << minoperation;
}
 
// Driver Code
int main()
{
    // Given Input
    string a = "10010010", b = "00001000";
 
    // Function Call
    minimumOperation(a, b);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
     
// Function to find minimum operations
// required to convert string A to B
static void minimumOperation(String a, String b)
{
     
    // Store the size of the string
    int n = a.length();
    int i = 0;
 
    // Store the required result
    int minoperation = 0;
 
    // Traverse the string, a
    while (i < n)
    {
         
        // If a[i] is equal to b[i]
        if (a.charAt(i) == b.charAt(i))
        {
            i = i + 1;
            continue;
        }
 
        // Check if swapping adjacent
        // characters make the same-indexed
        // characters equal or not
        else if (a.charAt(i) == b.charAt(i + 1) && 
                 a.charAt(i + 1) == b.charAt(i) &&
                   i < n - 1)
        {
            minoperation++;
            i = i + 2;
        }
 
        // Otherwise, flip the current bit
        else if (a.charAt(i) != b.charAt(i))
        {
            minoperation++;
            i = i + 1;
        }
        else
        {
            ++i;
        }
    }
 
    // Print the minimum number of operations
    System.out.println(minoperation);
}
 
// Driver Code
public static void main(String []args)
{
     
    // Given Input
    String a = "10010010", b = "00001000";
 
    // Function Call
    minimumOperation(a, b);
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to find minimum operations
# required to convert A to B
def minimumOperation(a, b):
 
    # Store the size of the string
    n = len(a)
    i = 0
 
    # Store the required result
    minoperation = 0
 
    # Traverse the string, a
    while (i < n):
 
        # If a[i] is equal to b[i]
        if (a[i] == b[i]):
            i = i + 1
            continue
 
        # Check if swapping adjacent
        # characters make the same-indexed
        # characters equal or not
        elif (a[i] == b[i + 1] and
              a[i + 1] == b[i] and i < n - 1):
            minoperation += 1
            i = i + 2
 
        # Otherwise, flip the current bit
        elif (a[i] != b[i]):
            minoperation += 1
            i = i + 1
        else:
            i+=1
 
    # Print the minimum number of operations
    print (minoperation)
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    a = "10010010"
    b = "00001000"
 
    # Function Call
    minimumOperation(a, b)
     
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find minimum operations
// required to convert string A to B
static void minimumOperation(string a, string b)
{
     
    // Store the size of the string
    int n = a.Length;
    int i = 0;
 
    // Store the required result
    int minoperation = 0;
 
    // Traverse the string, a
    while (i < n)
    {
         
        // If a[i] is equal to b[i]
        if (a[i] == b[i])
        {
            i = i + 1;
            continue;
        }
 
        // Check if swapping adjacent
        // characters make the same-indexed
        // characters equal or not
        else if (a[i] == b[i + 1] && 
                 a[i + 1] == b[i] &&
                   i < n - 1)
        {
            minoperation++;
            i = i + 2;
        }
 
        // Otherwise, flip the current bit
        else if (a[i] != b[i])
        {
            minoperation++;
            i = i + 1;
        }
        else
        {
            ++i;
        }
    }
 
    // Print the minimum number of operations
    Console.WriteLine(minoperation);
}
 
// Driver Code
public static void Main()
{
     
    // Given Input
    string a = "10010010", b = "00001000";
 
    // Function Call
    minimumOperation(a, b);
}
}
 
// This code is contributed by ankThon


Javascript


输出:
3

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

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