Related Articles
Sum of frequencies of characters of a string present in another string
• Last Updated : 13 Apr, 2021

Given two strings S1 and S2 of lengths M and N respectively, the task is to calculate the sum of the frequencies of the characters of string S1 in the string S2.

Examples:

Input: S1 = “pPKf”, S2 = “KKKttsdppfP”
Output: 7
Explanation:
The character ‘p’ occurs twice in the string S2.
The character ‘P’ occurs once in the string S2.
The character ‘K’ occurs thrice in the string S2.
The character ‘f’ occurs once in the string S2.
Therefore, in total, characters of the string S1 occurs 7 times in the string S2.

Input: S1 = “geEksFOR”, S2 = “GeEksforgeEKS”
Output: 7

Naive Approach: The simplest approach is to iterate over each character of the string S1, count its frequency in the string S2
Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized by using Hashing. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++

 `// CPP 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<``char``> 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<``char``> bset = ``new` `HashSet<``char``>();`   `    ``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`

Output:

`7`

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :