📌  相关文章
📜  使两个字符串K 等价所需的最少插入或删除次数

📅  最后修改于: 2021-10-27 06:54:50             🧑  作者: Mango

给定两个字符串str1STR2,任务是找到操作的最小数量要求任一插入一个字符到STR2或通过去除一个字符字符串STR1K(<1000)将字符串STR2的类似的字符中的每个字符映射从str2

例子:

方法:为了解决这个问题,选择K的最佳值,使得所需的操作次数最少。要通过选择K的最佳值,迭代k的所有可能的值,并保持业务为他们每个人所需要的计数的轨道。最后,打印获得的最小计数。请按照以下步骤解决问题:

  1. 初始化两个数组,比如a1[]a2[] ,分别存储str1str2的每个不同字符的频率。
  2. 迭代K 的所有可能值并跟踪ans 中的最小值:
    • 迭代范围[0, 25]并检查以下条件:
      • 如果str1 中不存在某个字符而str2 中存在某个字符,则应从str2 中删除所有这些字符。因此,应相应地更新计数
      • 字符是否存在于STR1,然后更新通过在STR2该字符和K的当前值的频率的绝对差的计数乘以在STR1该字符的频率。
    • count更新ans
  3. 最后打印得到的count,即ans

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum count of operations
// required to map each character of str1 to K
// same characters of str2
int minOperations(string str1, string str2)
{
 
    // Store frequency of each
    // distinct character of str1
    int a1[26] = { 0 };
 
    // Store frequency of each
    // distinct character of str2
    int a2[26] = { 0 };
 
    // Iterate over each character
    // of the string, str1
    for (auto x : str1) {
 
        // Update frequency of
        // current character
        a1[x - 'a']++;
    }
 
    // Iterate over each character
    // of the string, str2
    for (auto x : str2) {
 
        // Update frequency of
        // current character
        a2[x - 'a']++;
    }
 
    // Stores minimum count of operations
    // required to map each character of
    // str1 to K same characters of str2
    int ans = INT_MAX;
 
    // Iterate over all
    // possible values of K
    for (int k = 1; k < 1001; k++) {
 
        // Stores count of operations required
        // to map each character of str1 to k
        // same characters of str2
        int count = 0;
 
        // Iterate over possible characters
        for (int i = 0; i < 26; i++) {
 
            // If (i + 'a') is not
            // present in str1
            if (a1[i] == 0) {
 
                // Update count by removing
                // character from str2
                count += a2[i];
            }
 
            // If a character is
            // present in str1
            else {
 
                // Update count by inserting
                // character into str2
                count += abs(a1[i] * k - a2[i]);
            }
        }
 
        // Update the answer
        ans = min(ans, count);
    }
    return ans;
}
 
// Driver Code
int main()
{
    string str1 = "aaa";
    string str2 = "bbb";
 
    // Function Call
    cout << minOperations(str1, str2) << endl;
 
    return 0;
}


Java
// Java program for the above approach
class GFG
{
  // Function to find minimum count of operations
  // required to map each character of str1 to K
  // same characters of str2
  static int minOperations(String str1, String str2)
  {
 
    // Store frequency of each
    // distinct character of str1
    int[] a1 = new int[26];
 
    // Store frequency of each
    // distinct character of str2
    int[] a2 = new int[26];
 
    // Iterate over each character
    // of the string, str1
    for (int x = 0; x < str1.length(); x++)
    {
 
      // Update frequency of
      // current character
      a1[str1.charAt(x) - 'a']++;
    }
 
    // Iterate over each character
    // of the string, str2
    for (int x = 0; x < str2.length(); x++)
    {
 
      // Update frequency of
      // current character
      a2[str2.charAt(x) - 'a']++;
    }
 
    // Stores minimum count of operations
    // required to map each character of
    // str1 to K same characters of str2
    int ans = Integer.MAX_VALUE;
 
    // Iterate over all
    // possible values of K
    for (int k = 1; k < 1001; k++)
    {
 
      // Stores count of operations required
      // to map each character of str1 to k
      // same characters of str2
      int count = 0;
 
      // Iterate over possible characters
      for (int i = 0; i < 26; i++)
      {
 
        // If (i + 'a') is not
        // present in str1
        if (a1[i] == 0)
        {
 
          // Update count by removing
          // character from str2
          count += a2[i];
        }
 
        // If a character is
        // present in str1
        else
        {
 
          // Update count by inserting
          // character into str2
          count += Math.abs(a1[i] * k - a2[i]);
        }
      }
 
      // Update the answer
      ans = Math.min(ans, count);
    }
    return ans;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    String str1 = "aaa";
    String str2 = "bbb";
 
    // Function Call
    System.out.println(minOperations(str1, str2));
  }
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python program for the above approach
 
# Function to find minimum count of operations
# required to map each character of str1 to K
# same characters of str2
import sys
def minOperations(str1, str2):
 
  # Store frequency of each
    # distinct character of str1
    a1 = [0] * 26;
 
    # Store frequency of each
    # distinct character of str2
    a2 = [0] * 26;
 
    # Iterate over each character
    # of the string, str1
    for x in range(len(str1)):
 
        # Update frequency of
        # current character
        a1[ord(str1[x]) - ord('a')] += 1;
 
    # Iterate over each character
    # of the string, str2
    for x in range(len(str2)):
 
        # Update frequency of
        # current character
        a2[ord(str2[x]) - ord('a')] += 1;
 
    # Stores minimum count of operations
    # required to map each character of
    # str1 to K same characters of str2
    ans = sys.maxsize;
 
    # Iterate over all
    # possible values of K
    for k in range(1, 1001):
 
        # Stores count of operations required
        # to map each character of str1 to k
        # same characters of str2
        count = 0;
 
        # Iterate over possible characters
        for i in range(26):
 
            # If (i + 'a') is not
            # present in str1
            if (a1[i] == 0):
 
                # Update count by removing
                # character from str2
                count += a2[i];
 
 
            # If a character is
            # present in str1
            else:
 
                # Update count by inserting
                # character into str2
                count += abs(a1[i] * k - a2[i]);
 
        # Update the answer
        ans = min(ans, count);
    return ans;
 
# Driver code
if __name__ == '__main__':
    str1 = "aaa";
    str2 = "bbb";
 
    # Function Call
    print(minOperations(str1, str2));
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find minimum count of operations
// required to map each character of str1 to K
// same characters of str2
static int minOperations(string str1, string str2)
{
     
    // Store frequency of each
    // distinct character of str1
    int[] a1 = new int[26];
   
    // Store frequency of each
    // distinct character of str2
    int[] a2 = new int[26];
   
    // Iterate over each character
    // of the string, str1
    foreach(char x in str1)
    {
         
        // Update frequency of
        // current character
        a1[x - 'a']++;
    }
   
    // Iterate over each character
    // of the string, str2
    foreach(char x in str2)
    {
         
        // Update frequency of
        // current character
        a2[x - 'a']++;
    }
   
    // Stores minimum count of operations
    // required to map each character of
    // str1 to K same characters of str2
    int ans = Int32.MaxValue;
   
    // Iterate over all
    // possible values of K
    for(int k = 1; k < 1001; k++)
    {
         
        // Stores count of operations required
        // to map each character of str1 to k
        // same characters of str2
        int count = 0;
   
        // Iterate over possible characters
        for(int i = 0; i < 26; i++)
        {
             
            // If (i + 'a') is not
            // present in str1
            if (a1[i] == 0)
            {
                 
                // Update count by removing
                // character from str2
                count += a2[i];
            }
   
            // If a character is
            // present in str1
            else
            {
                 
                // Update count by inserting
                // character into str2
                count += Math.Abs(a1[i] * k - a2[i]);
            }
        }
         
        // Update the answer
        ans = Math.Min(ans, count);
    }
    return ans;
}
 
// Driver code
static void Main()
{
    string str1 = "aaa";
    string str2 = "bbb";
     
    // Function Call
    Console.WriteLine(minOperations(str1, str2));
}
}
 
// This code is contributed by divyesh072019


Javascript


输出:
6

时间复杂度: O(N + 26 * K)
辅助空间: O(N)

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