Given a string and a positive integer d, rearrange characters of the given string such that the same characters become at-least d distance away from each other.
Note that there can be many possible rearrangements, the output should be one of the possible rearrangements. If no such arrangement is possible, that should also be reported.
Expected time complexity is O(n) where n is length of input string.
Input: "aaaabbbcc", d = 2 Output: "ababacabc" Input: "aacbbc", d = 3 Output: "abcabc" Input: "geeksforgeeks", d = 3 Output: egkesfegkeors Input: "aaa", d = 2 Output: Cannot be rearranged
We have already discussed how to put same characters exactly d distance away. This is a extended version where same characters should be moved at-least d distance away.
The idea is to use extra space to store frequencies of all characters and maintain an array for inserting the values at correct distance. Following is the complete algorithm –
- Let the given string be str and size of string be n and alphabet size is be assumed as 256 (a constant).
- We scan input string str and store frequencies of all characters in an array freq.
- We create an array dist for inserting the values at correct distance. dist[j] will store the least distance between current position and the next position we can use character ‘j’.
If dist[j] <= 0, character ‘j’ can be inserted in current position.
- run a loop n times
- Search for next eligible character with maximum frequency and dist[j] <= 0.
- If we found such character, we put that character at next available position in output array, decrease its frequency and reset its distance as d. If we don’t find any character, string cannot be rearranged and we return false.
- As we move forward in output string, we decrement distance of all characters in dist by 1.
Following is the implementation of above algorithm.
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Rearrange a string so that all same characters become d distance away
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Check if frequency of all characters can become same by one removal
- 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 the characters of the string such that no two adjacent characters are consecutive English alphabets
- Min flips of continuous characters to make all characters same in a string
- Rearrange the given string such that all prime multiple indexes have same character
- Meta Strings (Check if two strings can become same after a swap in one string)
- Minimum characters to be replaced to make frequency of all characters same
- String with k distinct characters and no same characters adjacent
- Check whether two strings contain same characters in same order
- Remove minimum number of characters so that two strings become anagram
- Generate string with Hamming Distance as half of the hamming distance between strings A and B
- Check whether all the substrings have number of vowels atleast as that of consonants
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Check if a string can become empty by recursively deleting a given sub-string
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string has all characters with same frequency with one variation allowed