Given a string containing only lowercase english alphabets. The task is to find the bitwise XOR of all the prime frequencies of the characters in the string. If no prime frequency is present, then print -1.
Examples:
Input : str = "gggggeeekkkks" Output : 6 Input : str = "aabbbbw" Output : -1
Approach:
- Traverse the string and store the frequencies of all the characters using map STL.
- Find the frequencies which are prime numbers using Sieve Of Eratosthenes.
- Calculate XOR of all these prime frequencies.
Below is the implementation of the above approach:
C++
// C++ program to find XOR 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 XOR of prime frequencies int xorOfPrime(string s) { bool prime[100005]; memset(prime, true, sizeof(prime)); SieveOfEratosthenes(prime, 10005); int i, j; // map is used to store character // frequencies map<char, int> m; for (i = 0; i < s.length(); i++) m[s[i]]++; int result = 0; int flag = 0; // Traverse the map for (auto it = m.begin(); it != m.end(); it++) { // Calculate XOR of all prime // frequencies if (prime[it->second]) { result ^= it->second; flag = 1; } } if (!flag) return -1; return result; } // Driver code int main() { string s = "gggggeeekkkks"; cout << xorOfPrime(s); return 0; } |
Java
// Java program to find XOR of Prime // Frequencies of Characters in a String import java.util.*; class GFG { // Function to create Sieve to check primes static void SieveOfEratosthenes(boolean 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 XOR of prime frequencies static int xorOfPrime(char[] s) { boolean []prime = new boolean[100005]; for(int i = 0; i < 100005; i++) prime[i] = true; SieveOfEratosthenes(prime, 10005); int i, j; // map is used to store character // frequencies Map<Character,Integer> m = new HashMap<>(); for (i = 0; i < s.length; i++) { if(m.containsKey(s[i])) { m.put(s[i], m.get(s[i])+1); } else { m.put(s[i], 1); } } int result = 0; int flag = 0; // Traverse the map for (Map.Entry<Character,Integer> entry : m.entrySet()) { // Calculate XOR of all prime // frequencies if (prime[entry.getValue()]) { result ^= entry.getValue(); flag = 1; } } if (flag != 1) return -1; return result; } // Driver code public static void main(String[] args) { char[] s = "gggggeeekkkks".toCharArray(); System.out.println(xorOfPrime(s)); } } // This code has been contributed by 29AjayKumar |
Python3
# Python3 program to find XOR of Prime # Frequencies of Characters in a String from collections import defaultdict # Function to create Sieve to check primes def SieveOfEratosthenes(prime, p_size): # False here indicates # that it is not prime prime[0] = False prime[1] = False p = 2 while p * p <= p_size: # 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 i in range(p * 2, p_size + 1, p): prime[i] = False p += 1 # Function to find XOR of prime frequencies def xorOfPrime(s): prime = [True] * 100005 SieveOfEratosthenes(prime, 10005) # map is used to store character frequencies m = defaultdict(lambda:0) for i in range(0, len(s)): m[s[i]] += 1 result = flag = 0 # Traverse the map for it in m: # Calculate XOR of all prime frequencies if prime[m[it]]: result ^= m[it] flag = 1 if not flag: return -1 return result # Driver code if __name__ == "__main__": s = "gggggeeekkkks" print(xorOfPrime(s)) # This code is contributed by Rituraj Jain |
C#
// C# program to find XOR of Prime // Frequencies of Characters in a String using System; using System.Collections.Generic; class GFG { // Function to create Sieve to check primes static void SieveOfEratosthenes(Boolean []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 XOR of prime frequencies static int xorOfPrime(char[] s) { Boolean []prime = new Boolean[100005]; for(int i = 0; i < 100005; i++) prime[i] = true; SieveOfEratosthenes(prime, 10005); // map is used to store character // frequencies Dictionary<char, int> mp = new Dictionary<char,int>(); for (int i = 0; i < s.Length; i++) { if (mp.ContainsKey(s[i])) { var v = mp[s[i]] + 1; mp.Remove(s[i]); mp.Add(s[i], v); } else { mp.Add(s[i], 1); } } int result = 0; int flag = 0; // Traverse the map foreach(KeyValuePair<char, int> entry in mp) { // Calculate XOR of all prime // frequencies if (prime[entry.Value]) { result ^= entry.Value; flag = 1; } } if (flag != 1) return -1; return result; } // Driver code public static void Main(String[] args) { char[] s = "gggggeeekkkks".ToCharArray(); Console.WriteLine(xorOfPrime(s)); } } // This code contributed by Rajput-Ji |
6
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum and Product of Prime Frequencies of Characters in a String
- Check whether the frequencies of all the characters in a string are prime or not
- Remove characters from given string whose frequencies are a Prime Number
- Print characters having prime frequencies in order of occurrence
- Binary string with given frequencies of sums of consecutive pairs of characters
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Generate a Number in Decreasing order of Frequencies of characters of a given String
- Numbers with prime frequencies greater than or equal to k
- Array elements with prime frequencies
- Print characters having odd frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
- Print characters having even frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence using Binary Tree
- Queries for frequencies of characters in substrings
- Print characters and their frequencies in order of occurrence
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Generate a string with maximum possible alphabets with odd frequencies
- Queries to find frequencies of a string within specified substrings
- Program to find the XOR of ASCII values of characters 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.

