# Sum and Product of Prime Frequencies of Characters in a String

Given a string **str** containing only lowercase English alphabets, the task is to find the sum and product of all the prime frequencies of the characters in **str**.

**Examples:**

Input:str = “geeksforgeeks”

Output:6, 8

Only characters ‘g’, ‘k’ and ‘s’ have prime frequencies i.e. 2 + 2 + 2 = 6 and 2 * 2* 2 = 8

Character frequency g 2 e 4 k 2 s 2 f 1 o 1 r 1

Input:str = “algorithms”

Output:0, 0

**Approach:**

- Traverse the string and store the frequencies of all the characters in a hash table.
- Find the frequencies which are prime using Sieve Of Eratosthenes.
- Calculate the sum and product of all of these prime frequencies and finally print the sum and product.

Below is the implementation of the above approach:

`// C++ program to find Sum and product of Prime ` `// Frequencies of Characters in a String ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to create Sieve to check primes ` `void` `SieveOfEratosthenes(` `bool` `prime[], ` `int` `p_size) ` `{ ` ` ` `// false here indicates ` ` ` `// that it is not prime ` ` ` `prime[0] = ` `false` `; ` ` ` `prime[1] = ` `false` `; ` ` ` ` ` `for` `(` `int` `p = 2; p * p <= p_size; p++) { ` ` ` ` ` `// If prime[p] is not changed, ` ` ` `// then it is a prime ` ` ` `if` `(prime[p]) { ` ` ` ` ` `// Update all multiples of p, ` ` ` `// set them to non-prime ` ` ` `for` `(` `int` `i = p * 2; i <= p_size; i += p) ` ` ` `prime[i] = ` `false` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to find the sum of prime frequencies ` `// of the characters of the given string ` `void` `sumProdOfPrimeFreq(string s) ` `{ ` ` ` `bool` `prime[s.length() + 1]; ` ` ` `memset` `(prime, ` `true` `, ` `sizeof` `(prime)); ` ` ` ` ` `SieveOfEratosthenes(prime, s.length() + 1); ` ` ` ` ` `int` `i, j; ` ` ` ` ` `// map is used to store ` ` ` `// character frequencies ` ` ` `unordered_map<` `char` `, ` `int` `> m; ` ` ` `for` `(i = 0; i < s.length(); i++) ` ` ` `m[s[i]]++; ` ` ` ` ` `int` `sum = 0, product = 1; ` ` ` ` ` `// Traverse the map ` ` ` `for` `(` `auto` `it = m.begin(); it != m.end(); it++) { ` ` ` ` ` `// If the frequency is prime ` ` ` `if` `(prime[it->second]) { ` ` ` `sum += it->second; ` ` ` `product *= it->second; ` ` ` `} ` ` ` `} ` ` ` ` ` `cout << ` `"Sum = "` `<< sum; ` ` ` `cout << ` `"\nProduct = "` `<< product; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"geeksforgeeks"` `; ` ` ` ` ` `sumProdOfPrimeFreq(s); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Sum = 6 Product = 8

## Recommended Posts:

- XOR of Prime Frequencies of Characters in a String
- Check whether the frequencies of all the characters in a string are prime or not
- Python | Check if frequencies of all characters of a string are different
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Count characters in a string whose ASCII values are prime
- Check whether count of distinct characters in a string is Prime or not
- Program to find the product of ASCII values of characters in a string
- Program to print characters present at prime indexes in a given string
- Queries for frequencies of characters in substrings
- Print characters having odd frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence
- Array elements with prime frequencies
- Numbers with prime frequencies greater than or equal to k
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Min flips of continuous characters to make all characters same in a string

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.