📌  相关文章
📜  当前字符串的字符的频率在另一个字符串总结

📅  最后修改于: 2021-10-27 08:27:17             🧑  作者: Mango

给定两个长度分别为MN 的字符串S1S2 ,任务是计算字符串字符出现的频率之和 S1的字符串S2英寸

例子:

朴素的方法:最简单的方法是遍历字符串S1 的每个字符,计算其在字符串S2 中的频率。

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

高效的方法:上述方法可以通过使用 Hashing 进行优化。请按照以下步骤解决问题:

  • 初始化整数计数以存储所需的总和。
  • 将字符串S1 的所有字符插入一个 Set 中,比如bset
  • 遍历字符串S2和检查的字符,如果当前字符存在于该组,BSET,或没有。如果发现为真,则将计数加一。
  • 完成以上步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to find sum of frequencies
// of characters of S1 present in S2
void countTotalFrequencies(string S1, string S2)
{
 
  // Insert all characters of
  // string S1 in the set
  set bset;
  for(auto x:S1)
    bset.insert(x);
  int count = 0;
 
  // Traverse the string S2
  for (auto x: S2)
  {
 
    // Check if X is present
    // in bset or not
    if (bset.find(x) != bset.end())
 
      // Increment count by 1
      count += 1;
  }
 
  // Finally, print the count
  cout << count << endl;
 
}
 
// Driver Code
int main()
{
   
  // Given strings
  string S1 = "geEksFOR";
  string S2 = "GeEksforgeEKS";
 
  countTotalFrequencies(S1, S2);
 
}
 
// This code is contributed by ipg2016107.


Java
// Java program for the above approach
import java.util.HashSet;
 
class GFG{
 
// Function to find sum of frequencies
// of characters of S1 present in S2
static void countTotalFrequencies(String S1, String S2)
{
     
    // Insert all characters of
    // string S1 in the set
    HashSet bset = new HashSet();
    char[] S1arr = S1.toCharArray();
    char[] S2arr = S2.toCharArray();
     
    for(char x : S1arr)
        bset.add(x);
         
    int count = 0;
 
    // Traverse the string S2
    for(char x : S2arr)
    {
         
        // Check if X is present
        // in bset or not
        if (bset.contains(x))
 
            // Increment count by 1
            count += 1;
    }
 
    // Finally, print the count
    System.out.print(count);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given strings
    String S1 = "geEksFOR";
    String S2 = "GeEksforgeEKS";
 
    countTotalFrequencies(S1, S2);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to find sum of frequencies
# of characters of S1 present in S2
def countTotalFrequencies(S1, S2):
 
    # Insert all characters of
    # string S1 in the set
    bset = set(S1)
    count = 0
 
    # Traverse the string S2
    for x in S2:
 
        # Check if X is present
        # in bset or not
        if x in bset:
 
            # Increment count by 1
            count += 1
 
    # Finally, print the count
    print(count)
 
# Driver Code
 
# Given strings
S1 = "geEksFOR"
S2 = "GeEksforgeEKS"
 
countTotalFrequencies(S1, S2)


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
     
// Function to find sum of frequencies
// of characters of S1 present in S2
static void countTotalFrequencies(string S1,
                                  string S2)
{
 
    // Insert all characters of
    // string S1 in the set
    HashSet bset = new HashSet();
 
    foreach(char x in S1)
        bset.Add(x);
         
    int count = 0;
 
    // Traverse the string S2
    foreach(char x in S2)
    {
 
        // Check if X is present
        // in bset or not
        if (bset.Contains(x))
 
            // Increment count by 1
            count += 1;
    }
     
    // Finally, print the count
    Console.Write(count);
}
 
// Driver code
static void Main()
{
     
    // Given strings
    string S1 = "geEksFOR";
    string S2 = "GeEksforgeEKS";
 
    countTotalFrequencies(S1, S2);
}
}
 
// This code is contributed by abhinavjain194


Javascript


输出:
7

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

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