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:
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.
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Program to print characters present at prime indexes in a given string
- Rearrange characters in a string such that no two adjacent are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- Rearrange string such that no pair of adjacent characters are of the same type
- Rearrange a string so that all same characters become atleast d distance away
- Rearrange a string so that all same characters become d distance away
- Replace all occurrences of character X with character Y in given string
- Maximize partitions such that no two substrings have any common character
- Find a string such that every character is lexicographically greater than its immediate next character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Rearrange a string in the form of integer sum followed by the minimized character
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Check if both halves of the string have at least one different character
- Minimum string such that every adjacent character of given string is still adjacent
- Rearrange given string to maximize the occurrence of string t
- Check if both halves of the string have same set of characters
- Check if both halves of the string have same set of characters in Python
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
Improved By : AnkitRai01