Home >Backend Development >C++ >The sum of the frequencies of characters in one string in another string

The sum of the frequencies of characters in one string in another string

王林
王林forward
2023-09-01 15:37:13726browse

The sum of the frequencies of characters in one string in another string

In this article, we will explore an interesting issue related to string manipulation in C. The problem statement is "the sum of the frequencies with which characters in one string occur in another string". This question provides a great opportunity to enhance your understanding of string manipulation, character frequency calculations, and mapping concepts in C.

Problem Statement

Given two strings, the task is to find the sum of the frequencies of occurrences of characters in the first string in the second string.

C Solution Method

To solve this problem, we first create a frequency map for the two strings using a hash map. A frequency plot is a map of each character in a string to the count of that character in the string. For this we will use STL unordered_map. After creating the frequency plot, we iterate over the frequency plot of the first string and for each character that is also present in the second string, we add its frequency to our sum.

Example

This is the C code to implement this solution -

#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;

int sumOfFrequencies(string str1, string str2) {
   unordered_map<char, int> freq1, freq2;
   
   for (char c : str1) {
      freq1[c]++;
   }
   
   for (char c : str2) {
      freq2[c]++;
   }
   
   int sum = 0;
   for (auto& kv : freq1) {
      if (freq2.count(kv.first)) {
         sum += kv.second;
      }
   }
   
   return sum;
}

int main() {
   string str1 = "hello", str2 = "world";
   cout << "The sum of frequencies is: " << sumOfFrequencies(str1, str2);
   return 0;
}   

Output

The sum of frequencies is: 3

Test case description

Let's consider the strings "hello" and "world".

When we pass these strings to the sumOfFrequencies function, it first creates a frequency map for the two strings. For "hello", the frequency mapping is {'h':1, 'e':1, 'l':2, 'o':1}, and for "world", the frequency mapping is {'w':1, ' o':1, 'r':1, 'l':1, 'd':1}.

The function then iterates over the frequency map of "hello" and for each character that also appears in "world" it adds its frequency to the sum. The common characters are 'o' and 'l', whose frequencies in "hello" are 1 and 2 respectively. Therefore, the sum of frequencies is 3.

Thus, the output of this program will be "The sum of frequencies is: 3".

in conclusion

This question provides us with a great opportunity to understand and practice the concept of frequency mapping in C. This is a great question to improve your C coding skills and understand how to work with strings and maps to solve problems.

The above is the detailed content of The sum of the frequencies of characters in one string in another string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete