Given a string and a positive integer d. Some characters may be repeated in the given string. Rearrange characters of the given string such that the same characters become 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.
Examples: Input: "abb", d = 2 Output: "bab" Input: "aacbbc", d = 3 Output: "abcabc" Input: "geeksforgeeks", d = 3 Output: egkegkesfesor Input: "aaa", d = 2 Output: Cannot be rearranged
Hint: Alphabet size may be assumed as constant (256) and extra space may be used.
Solution: The idea is to count frequencies of all characters and consider the most frequent character first and place all occurrences of it as close as possible. After the most frequent character is placed, repeat the same process for remaining characters.
1) Let the given string be str and size of string be n
2) Traverse str, store all characters and their frequencies in a Max Heap MH. The value of frequency decides the order in MH, i.e., the most frequent character is at the root of MH.
3) Make all characters of str as ‘\0’.
4) Do following while MH is not empty.
…a) Extract the Most frequent character. Let the extracted character be x and its frequency be f.
…b) Find the first available position in str, i.e., find the first ‘\0’ in str.
…c) Let the first position be p. Fill x at p, p+d,.. p+(f-1)d
Following are C++ and Python implementations of above algorithm.
Algorithmic Paradigm: Greedy Algorithm
Time Complexity: Time complexity of above implementation is O(n + mLog(MAX)). Here n is the length of str, m is count of distinct characters in str and MAX is maximum possible different characters. MAX is typically 256 (a constant) and m is smaller than MAX. So the time complexity can be considered as O(n).
The above code can be optimized to store only m characters in heap, we have kept it this way to keep the code simple. So the time complexity can be improved to O(n + mLogm). It doesn’t much matter through as MAX is a constant.
Also, the above algorithm can be implemented using a O(mLogm) sorting algorithm. The first steps of above algorithm remain same. Instead of building a heap, we can sort the freq array in non-increasing order of frequencies and then consider all characters one by one from sorted array.
We will soon be covering an extended version where same characters should be moved at least d distance away.
This article is contributed by Himanshu Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- 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
- Rearrange characters to form palindrome if possible
- Reduce Hamming distance by swapping two characters
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange given string to maximize the occurrence of string t
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Rearrange a string in sorted order followed by the integer sum
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Rearrange a binary string as alternate x and y occurrences
- Rearrange a string in the form of integer sum followed by the minimized character
- Rearrange the string to maximize the number of palindromic substrings
- Minimal moves to form a string by adding characters or appending string itself