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 the length of the input string.
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
The approach to solve this problem 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 the remaining characters.
- Let the given string be str and size of string be n
- Traverse str, store all characters and their frequencies in a Max Heap MH(implemented using priority queue). The value of frequency decides the order in MH, i.e., the most frequent character is at the root of MH.
- Make all characters of str as ‘\0’.
- Do the following while MH is not empty.
- Extract the Most frequent character. Let the extracted character be x and its frequency be f.
- Find the first available position in str, i.e., find the first ‘\0’ in str.
- Let the first position be p. Fill x at p, p+d,.. p+(f-1)d
Below is the implementation of the 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.
This article is contributed by RAVI TEJA NAMA. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Rearrange a string so that all same characters become atleast d distance away
- Check if frequency of all characters can become same by one removal
- Python Program to remove elements that are less than K difference away in a list
- 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
- Meta Strings (Check if two strings can become same after a swap in one string)
- Rearrange the given string such that all prime multiple indexes have same character
- Minimum characters to be replaced to make frequency of all characters same
- String with k distinct characters and no same characters adjacent
- Python dictionary, set and counter to check if frequencies can become same
- 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 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 whether two strings contain same characters in same order
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Check if a string has all characters with same frequency with one variation allowed