Last Updated: 19 Dec, 2020

Rearrange string

Hard
Asked in companies
eBayAdobeMicrosoft

Problem statement

You are given a string “S”. Your task is to rearrange the characters of a string “S”, such that it does not contain any two adjacent characters which are the same.

If it is possible to rearrange the string “S”, then print any possible arrangement. else, print “not possible” without quotes.

For Example:

For a string  “qaacde”, This string has two same adjacent characters.
So, one possible way to rearrange the string is “qacade”. Now, this string does not have two adjacent characters that are the same. 
Input format :
The first line of input contains a single integer T, representing the number of test cases or queries to be run. 

Then the T test cases follow.

The first line of each test case contains a string S.
Output format :
For each test case, the output will be “Yes” if you have returned the correct answer, else it will be “No”.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10    
0 <= |S| <= 10^5 

Time Limit: 1 sec

Approaches

01 Approach

In this approach, we will generate all the possible rearrangements of the string and check if the current string in our rearrangement does not have any two adjacent characters that are the same. If we can find any string which satisfies our criteria then we will return that string else we will return “not possible”.

 

We can implement the above approach by – 

  1. Generate all permutations of a string.
  2. For each permutation check if this permutation does not have two characters that are the same, if yes then we will return the current permutation else we will move to the next permutation.
  3. If we are not able to find any valid arrangement, then we will return “not possible”.

02 Approach

The idea is here to store the frequency of each character in a hashmap and then build a max heap of pairs containing the frequency of character and character.

We will always pick the highest frequency character that is not the same as the previously picked character from the max heap and add the picked character to the answer.

 

Steps : 

  1. Declare an empty string as an ANSWER.
  2. Store frequency of each character in a hashmap or array, key as a character, and value as its frequency.
  3. Build a max heap of pairs where each pair contains the frequency of character as a key and character as value.
  4. Declare a variable PREV to store the previously picked element and initialize it with -1.
  5. Run a loop until the max heap is not empty and do
    1. Pop the top element from max heap and add the character of the popped element to ANSWER.
    2. Decrease frequency of popped element by 1.
    3. Push the previous element back to max heap if its frequency is greater than 0.
    4. Make a popped element as the previous element for the next iteration.
  6. If the length of the original string and ANSWER is not equal then return “not possible”.
  7. Else return the ANSWER.

03 Approach

The idea is here to sort the string based on the frequency of characters and then merge characters one by one from the front and middle of the string using left and right pointers.

 

Steps : 

  1. Declare an empty string as an answer.
  2. Store the frequency of each character in a hashmap.
  3. Sort the given string based on the frequency of characters.
  4. Initialize left and right pointer as LEFT = 0 and RIGHT = (N – 1)/2 + 1, where N is length of string.
  5. Run a loop until LEFT<=(N-1)/2 and do :
    1. Add S[LEFT]  to answer and increment LEFT by 1 i.e. do LEFT = LEFT +1.
    2. If RIGHT < N then, Add S[RIGHT] to ANSWER and increment RIGHT by 1 i.e. do RIGHT = RIGHT + 1.
  6. Check if the ANSWER has any two adjacent characters that are the same, if yes then return “not possible” else return an ANSWER.

04 Approach

The idea here is to store the frequency of each character of a string in a frequency array.

Let’s consider the following two observations:

  1. We can fill the same characters in either even positions or odd positions of string.
  2. The order of filling characters does not matter except for the highest frequency character.
  3. Let us denote the length of the string as N. If the frequency of the highest frequency element in the string is greater than (N+1)/2 then we cannot generate a string that does not have any two same characters.

So we will fill maximum frequency characters first in even positions and then remaining characters at odd positions. If all even positions are filled then we will start filling from odd positions. 

 

Steps : 

  1. Declare an empty string as an ANSWER.
  2. Declare an array of size 26 as HASH[26] to store the frequency of each character.
  3. Store frequency of each character of a string in HASH.
  4. Calculate the maximum frequency character using HASH. Let us denote it by MAX_FREQUENCY_CHARACTER.
  5. If HASH[MAX_FREQUENCY_CHARACTER] > (N+1)/2 then return “not possible”.
  6. Store all characters of MAX_FREQUENCY in even positions of ANSWER.
  7. Store remaining characters in even position of ANSWER and if all even positions are filled then use odd positions to store characters in ANSWER.
  8. Return ANSWER.