📌  相关文章
📜  尽量减少使两个给定字符串回文所需的相同索引字符的替换或交换

📅  最后修改于: 2021-04-22 03:23:30             🧑  作者: Mango

给定两个字符串(分别由N个小写字母组成的str1str2) ,任务是找到以下两种类型的最小操作数,以使这两个字符串均为回文字符串。

  • 替换字符串的任何字符的任何其他字符([A – Z])。
  • 互换任意两个字符同时出现在字符串同指数。

例子:

方法:请按照以下步骤解决问题:

  • 初始化两个变量,例如i = 0j = 0 ,分别存储两个字符串的左指针和右指针的索引。
  • 初始化一个变量,例如cntOp,以存储使两个字符串回文字符串所需的最少操作数。
  • 遍历字符串并检查以下条件:
    • 如果str1 [i] == str1 [j]str2 [i]!= str2 [j],则将str2 [i]的值替换为str2 [j],并将cntOp的值增加1
    • 如果str1 [i]!= str1 [j]str2 [i] == str2 [j],则将str1 [i]的值替换为str1 [j],并将cntOp的值增加1
    • 如果str1 [i]!= str1 [j]str2 [i]!= str2 [j],则检查(str1 [i] == str2 [j]str2 [i] == str1 [j]的值])是否等于true。如果确定为true,则交换(str1 [i],str2 [j])并将cntOp的值增加1
    • 否则,将str1 [i]替换为str1 [j] ,将str2 [i]替换为str2 [j],并将cntOp的值增加2
  • 最后,打印cntOp的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find minimum operations
// to make both the strings palindromic
int MincntBothPalin(string str1,
                    string str2, int N)
{
 
    // Stores index of
    // the left pointer
    int i = 0;
 
    // Stores index of
    // the right pointer
    int j = N - 1;
 
    // Stores count of minimum operations to
    // make both the strings palindromic
    int cntOp = 0;
 
    while (i < j) {
 
        // if str1[i] equal to str1[j]
        // and str2[i] not equal to str2[j]
        if (str1[i] == str1[j]
            && str2[i] != str2[j]) {
 
            // Update cntOp
            cntOp += 1;
        }
 
        // If str1[i] not equal to str1[j]
        // and str2[i] equal to str2[j]
        else if (str1[i] != str1[j]
                 && str2[i] == str2[j]) {
 
            // Update cntOp
            cntOp += 1;
        }
 
        // If str1[i] is not equal to str1[j]
        // and str2[i] is not equal to str2[j]
        else if (str1[i] != str1[j]
                 && str2[i] != str2[j]) {
 
            // If str1[i] is equal to str2[j]
            // and str2[i] is equal to str1[j]
            if (str1[i] == str2[j]
                && str2[i] == str1[j]) {
 
                // Update cntOp
                cntOp += 1;
            }
            else {
 
                // Update cntOp
                cntOp += 2;
            }
        }
 
        // Update i and j
        i += 1;
        j -= 1;
    }
 
    return cntOp;
}
 
// Driver Code
int main()
{
    string str1 = "dbba";
    string str2 = "abcd";
 
    // Stores length of str1
    int N = str1.length();
    cout << MincntBothPalin(
        str1, str2, N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find minimum operations
// to make both the Strings palindromic
static int MincntBothPalin(char[] str1,
                           char[] str2, int N)
{
     
    // Stores index of
    // the left pointer
    int i = 0;
 
    // Stores index of
    // the right pointer
    int j = N - 1;
 
    // Stores count of minimum operations to
    // make both the Strings palindromic
    int cntOp = 0;
 
    while (i < j)
    {
         
        // If str1[i] equal to str1[j]
        // and str2[i] not equal to str2[j]
        if (str1[i] == str1[j] &&
            str2[i] != str2[j])
        {
 
            // Update cntOp
            cntOp += 1;
        }
 
        // If str1[i] not equal to str1[j]
        // and str2[i] equal to str2[j]
        else if (str1[i] != str1[j] &&
                 str2[i] == str2[j])
        {
             
            // Update cntOp
            cntOp += 1;
        }
 
        // If str1[i] is not equal to str1[j]
        // and str2[i] is not equal to str2[j]
        else if (str1[i] != str1[j] &&
                 str2[i] != str2[j])
        {
             
            // If str1[i] is equal to str2[j]
            // and str2[i] is equal to str1[j]
            if (str1[i] == str2[j] &&
                str2[i] == str1[j])
            {
                 
                // Update cntOp
                cntOp += 1;
            }
            else
            {
                 
                // Update cntOp
                cntOp += 2;
            }
        }
 
        // Update i and j
        i += 1;
        j -= 1;
    }
    return cntOp;
}
 
// Driver Code
public static void main(String[] args)
{
    String str1 = "dbba";
    String str2 = "abcd";
 
    // Stores length of str1
    int N = str1.length();
     
    System.out.print(MincntBothPalin(
        str1.toCharArray(), str2.toCharArray(), N));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to find minimum
# operations to make both
# the strings palindromic
def MincntBothPalin(str1,
                    str2, N):
 
    # Stores index of
    # the left pointer
    i = 0
 
    # Stores index of
    # the right pointer
    j = N - 1
 
    # Stores count of minimum
    # operations to make both
    # the strings palindromic
    cntOp = 0
 
    while (i < j):
 
        # if str1[i] equal to
        # str1[j] and str2[i]
        # not equal to str2[j]
        if (str1[i] == str1[j] and
            str2[i] != str2[j]):
 
            # Update cntOp
            cntOp += 1
 
        # If str1[i] not equal
        # to str1[j] and str2[i]
        # equal to str2[j]
        elif (str1[i] != str1[j] and
              str2[i] == str2[j]):
 
            # Update cntOp
            cntOp += 1
 
        # If str1[i] is not equal
        # to str1[j] and str2[i]
        # is not equal to str2[j]
        elif (str1[i] != str1[j] and
              str2[i] != str2[j]):
 
            # If str1[i] is equal to
            # str2[j] and str2[i] is
            # equal to str1[j]
            if (str1[i] == str2[j] and
                str2[i] == str1[j]):
 
                # Update cntOp
                cntOp += 1
 
            else:
 
                # Update cntOp
                cntOp += 2
 
        # Update i and j
        i += 1
        j -= 1
 
    return cntOp
 
# Driver Code
if __name__ == "__main__":
   
    str1 = "dbba"
    str2 = "abcd"
 
    # Stores length of str1
    N = len(str1)
    print(MincntBothPalin(str1,
                          str2, N))
 
# This code is contributed by Chitranayal


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find minimum operations
// to make both the strings palindromic
static int MincntBothPalin(string str1,
                           string str2, int N)
{   
  // Stores index of
  // the left pointer
  int i = 0;
 
  // Stores index of
  // the right pointer
  int j = N - 1;
 
  // Stores count of minimum
  // operations to make both
  // the strings palindromic
  int cntOp = 0;
 
  while (i < j)
  {
    // If str1[i] equal to
    // str1[j] and str2[i]
    // not equal to str2[j]
    if (str1[i] == str1[j] &&
        str2[i] != str2[j])
    {
      // Update cntOp
      cntOp += 1;
    }
 
    // If str1[i] not equal
    // to str1[j] and str2[i]
    // equal to str2[j]
    else if (str1[i] != str1[j] &&
             str2[i] == str2[j])
    {
      // Update cntOp
      cntOp += 1;
    }
 
    // If str1[i] is not equal
    // to str1[j] and str2[i]
    // is not equal to str2[j]
    else if (str1[i] != str1[j] &&
             str2[i] != str2[j])
    {
      // If str1[i] is equal
      // to str2[j] and str2[i]
      // is equal to str1[j]
      if (str1[i] == str2[j] &&
          str2[i] == str1[j])
      {
        // Update cntOp
        cntOp += 1;
      }
      else
      {
        // Update cntOp
        cntOp += 2;
      }
    }
 
    // Update i and j
    i += 1;
    j -= 1;
  }
  return cntOp;
}
 
// Driver Code
public static void Main()
{
  string str1 = "dbba";
  string str2 = "abcd";
 
  // Stores length of str1
  int N = str1.Length;
 
  Console.WriteLine(
  MincntBothPalin(str1,
                  str2, N));
}
}
 
// This code is contributed by bgangwar59


输出:
2








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