Given a string str of size N. The task is to find out whether it is possible to rearrange characters in string str so that for any prime number p <= N and for any integer i ranging from 1 to N/p the condition strp = strp*i must be satisfied. If it is not possible to for any such rearrangement then print -1.
Input : str = “aabaaaa”
Output : baaaaaa
Size of the string is 7.
Indexes 2, 4, 6 contains same character.
Indexes 3, 6 contains the same character.
Input : str = “abcd”
Output : -1
- All positions except the first and those whose number is a prime greater N/2 must have the same symbol.
- Remaining positions can have any symbol. This positions kept using the sieve.
- If the most occurred element in the string is less than these positions then print -1.
Below is the implementation of the above approach:
- Program to print characters present at prime indexes in a given string
- Rearrange a string in the form of integer sum followed by the minimized character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Find First element in AP which is multiple of given prime
- Rearrange given string to maximize the occurrence of string t
- Prime Factorization using Sieve O(log n) for multiple queries
- Map every character of one string to another such that all occurrences are mapped to the same character
- Replace every character of a string by a different character
- Rearrange characters in a string such that no two adjacent are same
- Rearrange a string so that all same characters become d distance away
- Rearrange a string in sorted order followed by the integer sum
- Rearrange a string so that all same characters become atleast d distance away
- Rearrange characters in a string such that no two adjacent are same using hashing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : AnkitRai01