📌  相关文章
📜  在不删除字符的情况下制作两个字符串 Anagram 所需的最小操作数 |设置 2

📅  最后修改于: 2022-05-13 01:57:07.659000             🧑  作者: Mango

在不删除字符的情况下制作两个字符串 Anagram 所需的最小操作数 |设置 2

给定两个大小相同的字符串s[]t[] ,大小为N 。在一个步骤中,选择t[]的任何字符并将其替换为另一个字符。返回使t[]成为s[ ] 的变位词的最小步数。

例子:

散列方法:散列方法已在本文的 Set 1 中讨论。在本文中,我们将研究如何使用地图解决此问题。

方法:想法是使用散列来存储字符串s[]的每个字符的频率,然后在遍历字符串t[]时,检查该字符是否存在于地图中。如果是,则将其值减1。否则,将计数加1。按照以下步骤解决问题:

  • 将变量count初始化为0以存储答案。
  • 初始化一个unordered_map a[]来存储频率。
  • 使用变量i迭代范围[0, N)并执行以下步骤:
    • a[i]的值增加1。
  • 使用变量j遍历字符串并执行以下任务:
    • 如果a[j]大于0 ,则将a[j]的值减1。
    • 否则,将count的值增加1。
  • 执行上述步骤后,打印count的值作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the minimum changes
// to make the 2 strings anagram
int minSteps(string s, string t)
{
    unordered_map a;
 
    // For counting the steps to be changed
    int count = 0;
    for (auto i : s) {
 
        // Increasing the count if the no.
        // is present
        a[i]++;
    }
    for (auto j : t) {
 
        // Checking if the element of s is
        // present in t or not
        if (a[j] > 0) {
 
            // If present than decrease the
            // no. in map by 1
            a[j]--;
        }
        else {
 
            // If not present
            // increase count by 1
            count++;
        }
    }
    cout << count;
 
    // Return count
    return count;
}
 
// Driver Code
int main()
{
    string s = "ddcf", t = "cedk";
 
    minSteps(s, t);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
  // Function to count the minimum changes
  // to make the 2 Strings anagram
  static int minSteps(String s, String t) {
    HashMap a =
      new HashMap();
 
    // For counting the steps to be changed
    int count = 0;
    for (char i : s.toCharArray()) {
 
      // Increasing the count if the no.
      // is present
      if (a.containsKey(i)) {
        a.put(i, a.get(i) + 1);
      } else {
        a.put(i, 1);
      }
 
    }
    for (char j : t.toCharArray()) {
 
      // Checking if the element of s is
      // present in t or not
      if (a.containsKey(j)) {
 
        // If present than decrease the
        // no. in map by 1
        a.put(j, a.get(j) - 1);
      } else {
 
        // If not present
        // increase count by 1
        count++;
      }
    }
    System.out.print(count);
 
    // Return count
    return count;
  }
 
  // Driver Code
  public static void main(String[] args) {
    String s = "ddcf", t = "cedk";
 
    minSteps(s, t);
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# python program for the above approach
 
# Function to count the minimum changes
# to make the 2 strings anagram
def minSteps(s, t):
 
    a = {}
 
    # For counting the steps to be changed
    count = 0
 
    for i in s:
 
        # Increasing the count if the no.
        # is present
        if i in a:
            a[i] += 1
        else:
            a[i] = 1
 
    for j in t:
 
                # Checking if the element of s is
                # present in t or not
        if j in a:
 
                        # If present than decrease the
                        # no. in map by 1
            a[j] -= 1
 
        else:
 
            # If not present
            # increase count by 1
            count += 1
 
    print(count)
 
    # Return count
    return count
 
# Driver Code
if __name__ == "__main__":
 
    s = "ddcf"
    t = "cedk"
 
    minSteps(s, t)
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG {
 
  // Function to count the minimum changes
  // to make the 2 Strings anagram
  static int minSteps(String s, String t) {
    Dictionary a =
      new Dictionary();
 
    // For counting the steps to be changed
    int count = 0;
    foreach (char i in s.ToCharArray()) {
 
      // Increasing the count if the no.
      // is present
      if (a.ContainsKey(i)) {
        a[i] = a[i] + 1;
      } else {
        a.Add(i, 1);
      }
 
    }
    foreach (char j in t.ToCharArray()) {
 
      // Checking if the element of s is
      // present in t or not
      if (a.ContainsKey(j)) {
 
        // If present than decrease the
        // no. in map by 1
        a[j] = a[j] - 1;
      } else {
 
        // If not present
        // increase count by 1
        count++;
      }
    }
    Console.Write(count);
 
    // Return count
    return count;
  }
 
  // Driver Code
  public static void Main(String[] args) {
    String s = "ddcf", t = "cedk";
 
    minSteps(s, t);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出
2

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