📌  相关文章
📜  找到使两个字符串相等所需的最少预处理次数

📅  最后修改于: 2021-06-25 20:21:34             🧑  作者: Mango

给定两个长度相等的字符串AB ,由小写英文字母组成。任务是计算在应用以下操作后,要使字符串A等于字符串B所需的字符串A的最少预处理移动次数:

  1. 选择任何索引i(0≤i i和b i
  2. 选择任何索引i(0≤i i和n – i – 1
  3. 选择任何索引i(0≤i i和b n – i – 1

在一个预处理的举动,你可以用英文字母的任何其它字符替换一个字符。

例子:

方法:让我们将两个字符串的所有字符都分成几组,以使每组中的字符都可以随着更改相互交换。因此,将有以下几组:{A 0 ,A n – 1 ,B 0 ,B n – 1 },{A 1 ,A n – 2 ,B 1 ,B n – 2 }等。由于这些组不会互相影响,因此我们可以计算每个组中的预处理移动数量,然后将其汇总。

如何确定一个组是否不需要任何预处理动作?
对于由2个字符组成的组(如果n为奇数,将有一个这样的组),这很容易–如果该组中的字符相等,则答案为0,否则为1。

为了确定由四个字符组成的组所需的预处理动作数量,我们可以使用以下事实:如果可以将该组中的字符分成几对,则该组不需要任何预处理动作。因此,如果该组包含四个相等的字符或两对相等的字符,则该组的答案为0。否则,我们可以检查仅替换A i和A n – i – 1的一个字符就足够了。如果是,则答案为1,否则为2。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum number of
// pre-processing moves required on string A
int Preprocess(string A, string B)
{
    // Length of the given strings
    int n = A.size();
  
    // To store the required answer
    int ans = 0;
  
    // Run a loop upto n/2
    for (int i = 0; i < n / 2; i++) {
  
        // To store frequency of 4 characters
        map mp;
        mp[A[i]]++;
        mp[A[n - i - 1]]++;
        mp[B[i]]++;
        mp[B[n - i - 1]]++;
        int sz = mp.size();
  
        // If size is 4
        if (sz == 4)
            ans += 2;
  
        // If size is 3
        else if (sz == 3)
            ans += 1 + (A[i] == A[n - i - 1]);
  
        // If size is 2
        else if (sz == 2)
            ans += mp[A[i]] != 2;
    }
  
    // If n is odd
    if (n % 2 == 1 && A[n / 2] != B[n / 2])
        ans++;
  
    return ans;
}
  
// Driver code
int main()
{
    string A = "abacaba", B = "bacabaa";
    cout << Preprocess(A, B);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
    // Function to return the minimum number of
    // pre-processing moves required on string A
    static int Preprocess(String A, String B)
    {
        // Length of the given strings
        int n = A.length();
      
        // To store the required answer
        int ans = 0;
      
        // Run a loop upto n/2
        for (int i = 0; i < n / 2; i++) 
        {
      
            // To store frequency of 4 characters
              
            HashMap mp = new HashMap<>();
              
            if(mp.containsKey(A.charAt(i)))
                mp.put(A.charAt(i), mp.get(A.charAt(i))+1);
            else
            mp.put(A.charAt(i), 1);
              
            if(mp.containsKey(A.charAt(n-i-1))) 
                mp.put(A.charAt(n-i-1), mp.get(A.charAt(n-i-1))+1);
            else
            mp.put(A.charAt(n-i-1), 1);
              
            if(mp.containsKey(B.charAt(i)))
                mp.put(B.charAt(i), mp.get(B.charAt(i))+1);
            else
            mp.put(B.charAt(i), 1);
          
            if(mp.containsKey(B.charAt(n-i-1))) 
                mp.put(B.charAt(n-i-1), mp.get(B.charAt(n-i-1))+1);
            else
            mp.put(B.charAt(n-i-1), 1);
          
            int sz = mp.size();
      
            // If size is 4
            if (sz == 4)
                ans += 2;
      
            // If size is 3
            else if (sz == 3)
                ans += 1 + (A.charAt(i) == A.charAt(n - i - 1) ? 1 : 0 );
      
            // If size is 2
            else if (sz == 2)
                ans += mp.get(A.charAt(i)) != 2 ? 1 : 0;
        }
      
        // If n is odd
        if (n % 2 == 1 && A.charAt(n / 2) != B.charAt(n / 2))
            ans++;
      
        return ans;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        String A = "abacaba", B = "bacabaa";
        System.out.println(Preprocess(A, B));
      
    }
  
}
  
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
  
# Function to return the minimum number of
# pre-processing moves required on string A
def Preprocess(A, B):
  
    # Length of the given strings
    n = len(A)
  
    # To store the required answer
    ans = 0
  
    # Run a loop upto n/2
    for i in range(n // 2):
  
        # To store frequency of 4 characters
        mp = dict()
          
        mp[A[i]] = 1
        if A[i] == A[n - i - 1]:
            mp[A[n - i - 1]] += 1
  
        if B[i] in mp.keys():
            mp[B[i]] += 1
        else:
            mp[B[i]] = 1
          
        if B[n - i - 1] in mp.keys():
            mp[B[n - 1 - i]] += 1
        else:
            mp[B[n - 1 - i]] = 1
  
        sz = len(mp)
  
        # If size is 4
        if (sz == 4):
            ans += 2
          
        # If size is 3    
        elif (sz == 3):
            ans += 1 + (A[i] == A[n - i - 1])
          
        # If size is 2
        elif (sz == 2):
            ans += mp[A[i]] != 2
      
    # If n is odd
    if (n % 2 == 1 and A[n // 2] != B[n // 2]):
        ans += 1
  
    return ans
  
# Driver code
A = "abacaba"
B = "bacabaa"
print(Preprocess(A, B))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
    // Function to return the minimum number of
    // pre-processing moves required on string A
    static int Preprocess(string A, string B)
    {
        // Length of the given strings
        int n = A.Length;
      
        // To store the required answer
        int ans = 0;
      
        // Run a loop upto n/2
        for (int i = 0; i < n / 2; i++) 
        {
      
            // To store frequency of 4 characters
              
            Dictionary mp = new Dictionary();
  
              
            if(mp.ContainsKey(A[i]))
                mp[A[i]]++;
            else
            mp[A[i]] = 1;
              
            if(mp.ContainsKey(A[n-i-1])) 
                mp[A[n - i - 1]]++;
            else
                mp[A[n - i - 1]] = 1;
              
              
            if(mp.ContainsKey(B[i]))
                mp[B[i]]++;
            else
            mp[B[i]] = 1;
              
            if(mp.ContainsKey(B[n-i-1])) 
                mp[B[n - i - 1]]++;
            else
                mp[B[n - i - 1]] = 1;
              
            int sz = mp.Count;
      
            // If size is 4
            if (sz == 4)
                ans += 2;
      
            // If size is 3
            else if (sz == 3)
                ans += 1 + (A[i] == A[n - i - 1] ? 1 : 0 );
      
            // If size is 2
            else if (sz == 2)
                ans += mp[A[i]] != 2 ? 1 : 0;
        }
      
        // If n is odd
        if (n % 2 == 1 && A[n / 2] != B[n / 2])
            ans++;
      
        return ans;
    }
      
    // Driver code
    public static void Main ()
    {
        string A = "abacaba", B = "bacabaa";
        Console.WriteLine(Preprocess(A, B));
      
    }
}
  
// This code is contributed by ihritik


PHP


输出:
4

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。