Given a string S of length N, the task is to find the minimum operations required to make the frequency of every distinct character prime. The frequency of a character can be increased or decreased by 1 in a single operation.
Input: S = “abba”
Explanation: There are two characters in the string S and the frequency of ‘a’ is 2 which is prime. The frequency of ‘b’ is 2 which is also a prime number.Hence no operations are required for this case.
Input: S = “aaaaaaaaa”
Explanation: The frequency of ‘a’ in the string is 9. Either remove 2 a’s from the string or add 2 a’s in the string to make the frequency of ‘a’ prime. Thus minimum number of operations needed is 2.
Find the frequency of each character in the string. Let the frequency be X. Find the prime number greater than X and less than X. Compare the difference between X and the two prime numbers found and choose the closest prime number. Add the difference between the closest prime number and X to the number of operations. Thus the minimum number of operations will be obtained. It is an inefficient approach as the lower and upper bound to find the prime number is not known.
Efficient Approach :
- Use the sieve algorithm to find all the prime numbers up to N and store them in an array.
- Find out the nearest lower prime number for all the numbers from i = 1 to N and store the difference between i and its nearest lower prime number in an array say arr1.
- Find out the nearest higher prime number for all the numbers from i = 1 to N and store the difference between i and its nearest higher prime number in an array say arr2.
- Traverse the string and find the frequency of all the distinct characters in the string and use an unordered_map to save the frequencies of these distinct characters.
- Let the frequency of any distinct character be X then find out the distance between X and the nearest prime number from the arrays arr1 and arr2.
- Choose the distance which is lesser between the two and add this distance to the number of operations.
- Do this for all the distinct characters and print the number of operations finally.
Below is the implementation of the above approach:
Time complexity: O(N * log(log(N)))
Auxiliary Space complexity: O(N)
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.
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Minimum characters required to be removed to make frequency of each character unique
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Minimum characters to be replaced to make frequency of all characters same
- Minimum deletions required to make frequency of each letter unique
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Minimum operations to make frequency of all characters equal K
- Print number of words, vowels and frequency of each character
- Longest sub-string having frequency of each character less than equal to k
- Printing frequency of each character just after its consecutive occurrences
- Print the frequency of each character in Alphabetical order
- Largest index for each distinct character in given string with frequency K
- Check if frequency of each character is equal to its position in English Alphabet
- Frequency of each character in a String using unordered_map in C++
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Min flips of continuous characters to make all characters same in a string
- Replace minimal number of characters to make all characters pair wise distinct
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.