Check whether the frequencies of all the characters in a string are prime or not
Given a string
, the task is to check if the frequencies of all the characters of the string are prime or not. If all the frequencies are prime then print
otherwise print
.
Examples:
Input: str = “geeksforgeeks”
Output: No
Character Frequency g 2 e 4 k 2 s 2 f 1 o 1 r 1 It is clear that only the frequencies of g, k and s are prime.
Input: str = “aabbbccccc”
Output: Yes
Approach: Find the frequencies of all the characters present in the string and store them in a map. Then check whether all the frequencies are prime or not, if all the frequency are prime then print
else
.
Below is the implementation of the above approach:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // function that returns true // if n is prime else false bool isPrime(int n) { int i; // 1 is not prime if (n == 1) return false; // check if there is any factor or not for (i = 2; i <= sqrt(n); i++) if (n % i == 0) return false; return true; } // function that returns true if // the frequencies of all the // characters of s are prime bool check_frequency(string s) { // create a map to store // the frequencies of characters map<char, int> m; for (int i = 0; i < s.length(); i++) // update the frequency m[s[i]]++; // check whether all the frequencies // are prime or not for (char ch = 'a'; ch <= 'z'; ch++) if (m[ch] > 0 && !isPrime(m[ch])) return false; return true; } // Driver code int main() { string s = "geeksforgeeks"; // if all the frequencies are prime if (check_frequency(s)) cout << "Yes" << endl; else cout << "No" << endl; return 0; } |
Java
import java.util.*; // Java implementation of above approach class GFG { // function that returns true // if n is prime else false static boolean isPrime(int n) { int i; // 1 is not prime if (n == 1) { return false; } // check if there is any factor or not for (i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } // function that returns true if // the frequencies of all the // characters of s are prime static boolean check_frequency(char[] s) { // create a map to store // the frequencies of characters HashMap<Character, Integer> m = new HashMap<Character, Integer>(); for (int i = 0; i < s.length; i++) // update the frequency { if (m.containsKey(s[i])) { m.put(s[i], m.get(s[i]) + 1); } else { m.put(s[i], 1); } } // check whether all the frequencies // are prime or not for (char ch = 'a'; ch <= 'z'; ch++) { if (m.get(ch) != null && m.get(ch) > 0 && !isPrime(m.get(ch))) { return false; } } return true; } // Driver code public static void main(String[] args) { String s = "geeksforgeeks"; // if all the frequencies are prime if (check_frequency(s.toCharArray())) { System.out.println("Yes"); } else { System.out.println("No"); } } } // This code contributed by Rajput-Ji |
Python3
# Python3 implementation of above approach import math as mt # function that returns true # if n is prime else false def isPrime(n): i = 2 # 1 is not prime if (n == 1): return False # check if there is any factor or not for i in range(2, mt.ceil(mt.sqrt(n))): if (n % i == 0): return False return True # function that returns true if the # frequencies of all the characters # of s are prime def check_frequency(s): # create a map to store # the frequencies of characters m = dict() for i in range(len(s)): # update the frequency if s[i] in m.keys(): m[s[i]] += 1 else: m[s[i]] = 1 # check whether all the frequencies # are prime or not for ch in m: if m[ch] > 0 and isPrime(m[ch]) == False: return False return True # Driver code s = "geeksforgeeks" # if all the frequencies are prime if (check_frequency(s)): print("Yes") else: print("No") # This code is contributed # by Mohit kumar 29 |
C#
// C# implementation of above approach using System; using System.Collections.Generic; class GFG { // function that returns true // if n is prime else false static bool isPrime(int n) { int i; // 1 is not prime if (n == 1) { return false; } // check if there is any factor or not for (i = 2; i <= Math.Sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } // function that returns true if // the frequencies of all the // characters of s are prime static bool check_frequency(char[] s) { // create a map to store // the frequencies of characters Dictionary<char, int> m = new Dictionary<char, int>(); for (int i = 0; i < s.Length; i++) // update the frequency { if (m.ContainsKey(s[i])) { var c = m[s[i]]+1; m.Remove(s[i]); m.Add(s[i], c); } else { m.Add(s[i], 1); } } // check whether all the frequencies // are prime or not for (char ch = 'a'; ch <= 'z'; ch++) { if (m.ContainsKey(ch) && m[ch] > 0 && !isPrime(m[ch])) { return false; } } return true; } // Driver code public static void Main(String[] args) { String s = "geeksforgeeks"; // if all the frequencies are prime if (check_frequency(s.ToCharArray())) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } /* This code contributed by PrinciRaj1992 */ |
No
Recommended Posts:
- XOR of Prime Frequencies of Characters in a String
- Sum and Product of Prime Frequencies of Characters in a String
- Print characters having prime frequencies in order of occurrence
- Check whether count of distinct characters in a string is Prime or not
- 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
- Program to print characters present at prime indexes in a given string
- Check whether second string can be formed from characters of first string
- Queries for frequencies of characters in substrings
- Print characters having even frequencies in order of occurrence
- Print characters having odd frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence
- Check if both halves of the string have same set of characters
- Quick way to check if all the characters of a string are same
- Array elements with prime frequencies
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.



