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 firstname.lastname@example.org. 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
- Return maximum occurring character in an input string
- Print all the duplicates in the input string
- Remove characters from the first string which are present in the second string
- Remove duplicates from a given string
- Print reverse of a string using recursion
- Write a program to print all permutations of a given string
- Divide a string in N equal parts
- Given a string, find its first non-repeating character
- Write a program to reverse an array or string
- Print list items containing all characters of a given word
- Reverse words in a given string
- Find the smallest window in a string containing all characters of another string
- Length of the longest substring without repeating characters
- Print all permutations with repetition of characters
- Interleaving of two given strings with no common characters