
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of Frequencies of Characters in a String Present in Another String
In this article, we are going to explore an interesting problem related to string manipulation using various programming languages. The problem statement is "Sum of frequencies of characters of a string present in another string". This problem provides a great opportunity to enhance your understanding of string operations, character frequency calculation, and the concept of mapping in C, C++, Java and Python.
Problem Statement
Given two strings, the task is to find the sum of frequencies of characters of the first string that are present in the second string.
Solution Approach
To solve this problem, we will first create frequency maps for both strings using hash maps. A frequency map is a map where each character in the string maps to the count of that character in the string. We'll use the STL unordered_map for this purpose. After creating the frequency maps, we will iterate over the frequency map of the first string, and for each character that is also present in the second string, we add its frequency to our sum.
Example
Following are the programs to implement the above approach
#include <stdio.h> #include <stdlib.h> #include <string.h> // Function to find the sum of frequencies of str 1 and str 2 int sumOfFrequencies(char* str1, char* str2) { int freq1[256] = {0}; int freq2[256] = {0}; for (int i = 0; i < strlen(str1); i++) { freq1[str1[i]]++; } // Traverse the string str2 for (int i = 0; i < strlen(str2); i++) { freq2[str2[i]]++; } int sum = 0; for (int i = 0; i < 256; i++) { if (freq1[i] != 0 && freq2[i] != 0) { sum += freq1[i]; } } return sum; } // Driver code int main() { // strings char str1[] = "hello"; char str2[] = "world"; // print the Output printf("The sum of frequencies is: %d\n", sumOfFrequencies(str1, str2)); return 0; }
Output
The sum of frequencies is: 3
#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
import java.util.HashMap; public class Main { // Function to find the sum of frequencies of str 1 and str 2 public static int sumOfFrequencies(String str1, String str2) { HashMap<Character, Integer> freq1 = new HashMap<>(); HashMap<Character, Integer> freq2 = new HashMap<>(); // Inserting all the characters of string str1 in the set for (char c : str1.toCharArray()) { freq1.put(c, freq1.getOrDefault(c, 0) + 1); } // Traverse the string str2 for (char c : str2.toCharArray()) { freq2.put(c, freq2.getOrDefault(c, 0) + 1); } int sum = 0; for (char key : freq1.keySet()) { if (freq2.containsKey(key)) { sum += freq1.get(key); } } return sum; } public static void main(String[] args) { // Strings String str1 = "hello"; String str2 = "world"; //Print the Output System.out.println("The sum of frequencies is: " + sumOfFrequencies(str1, str2)); } }
Output
The sum of frequencies is: 3
# Function to find the sum of frequencies of str 1 and str 2 def sum_of_frequencies(str1, str2): freq1 = {} freq2 = {} # Inserting all the characters of string str1 in the set for c in str1: freq1[c] = freq1.get(c, 0) + 1 # Traverse the string str2 for c in str2: freq2[c] = freq2.get(c, 0) + 1 total_sum = 0 for key, value in freq1.items(): if key in freq2: # Increment count by 1 total_sum += value return total_sum # Srings str1 = "hello" str2 = "world" # printing the Output print("The sum of frequencies is:", sum_of_frequencies(str1, str2))
Output
The sum of frequencies is: 3
Explanation with a Test Case
Let's consider the strings "hello" and "world".
When we pass these strings to the sumOfFrequencies function, it first creates the frequency maps for both strings. For "hello", the frequency map is {'h':1, 'e':1, 'l':2, 'o':1}, and for "world", the frequency map 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 is also present in "world", it adds its frequency to the sum. The common characters are 'o' and 'l', and their frequencies in "hello" are 1 and 2, respectively. So, the sum of frequencies is 3.
Therefore, the output of this program will be "The sum of frequencies is: 3".
Conclusion
This problem provides a great opportunity to understand and practice the concept of frequency mapping in various programming languages. It's an excellent problem to improve your coding skills and to understand how to handle strings and maps for problem-solving.