# Rearrange characters in a string such that no two adjacent are same in C++

We are given a string, let's say, str of any given length. The task is to rearrange the given string in such a manner that there won't be the same adjacent characters arranged together in the resultant string.

## Let us see various input output scenarios for this −

Input − string str = "itinn"

Output − Rearrangement of characters in a string such that no two adjacent are same is: initn.

Explanation − We are given a string type variable let’s say, str. Now we will rearrange the characters of an input string in such a manner that no two same characters occur at the same position i.e. shifting ‘nn’ because they are the same and adjacent to each other. So the final string will be ‘initn’.

Input − string str = "abbaabbaa"

Output − Rearrangement of characters in a string such that no two adjacent are same is: ababababa

Explanation − We are given a string type variable let’s say, str. Now we will rearrange the characters of an input string in such a manner that no two same characters occur at the same position i.e. shifting ‘bb’, ‘aa’, ‘bb’, ‘aa’ because they are the same and adjacent to each other. So the final string will be ‘ababababa’.

## Approach used in the below program is as follows

• Input a variable of string type, let’s say, str and calculate the size of a string and store it in a length named variable.

• Check IF length is 0 then return.

• Pass the data to the function Rearrangement(str, length).

• Inside the function Rearrangement(arr, length)

• Set size of a string with (length + 1)/2.

• Declare a vector type variable as vec(26, 0) that will store the integer type data and a ptr of string type as ptr(length, ‘ ‘). A temporary variable of type integer as 0.

• Start loop FOR to iterate str through it. Inside the loop, set vec[it - ‘a’]++.

• Create a character type variable as ch and set it with a call to the maximum(vec) function.

• Declare an integer type variable as total and set it with vec[ch - ‘a’].

• Check IF total greater than size then return.

• Start loop WHILE total then set ptr[temp] to ch, set temp to temp + 2 and decrement the total by 1.

• Set vec[ch - 'a'] to 0. Start loop FOR from i to 0 till i less than 26. Inside the loop, start while vec[i] is greater than 0. Set temp to (temp >= length) ? 1 : temp and ptr[temp] to 'a' + i and temp to temp + 2 and decrement the vec[i] by 1.

• Return ptr

• Inside the function char maximum(const vector<int>& vec)

• Declare an integer type variable as high to 0 and character type variable as ‘c’

• Start loop FOR from i to 0 till i less than 26. Inside the loop, check IF vec[i] is less than high then set high to vec[i] and c to 'a' + i.

• Return c

• Print the result.

## Example

#include <bits/stdc++.h>
using namespace std;
char maximum(const vector<int>& vec){
int high = 0;
char c;
for(int i = 0; i < 26; i++){
if(vec[i] > high){
high = vec[i];
c = 'a' + i;
}
}
return c;
}
string Rearrangement(string str, int length){
int size = (length + 1) / 2;
vector<int> vec(26, 0);
string ptr(length, ' ');
int temp = 0;
for(auto it : str){
vec[it - 'a']++;
}
char ch = maximum(vec);
int total = vec[ch - 'a'];
if(total > size){
return "";
}
while(total){
ptr[temp] = ch;
temp = temp + 2;
total--;
}
vec[ch - 'a'] = 0;
for(int i = 0; i < 26; i++){
while (vec[i] > 0){
temp = (temp >= length) ? 1 : temp;
ptr[temp] = 'a' + i;
temp = temp + 2;
vec[i]--;
}
}
return ptr;
}
int main(){
string str = "itinn";
int length = str.length();
if(length == 0){
}
string count = Rearrangement(str, length);
if(count == ""){
}
else{
cout<<"Rearrangement of characters in a string such that no two adjacent are same is: "<<count;
}
return 0;
}

## Output

If we run the above code it will generate the following Output

Rearrangement of characters in a string such that no two adjacent are same is: initn

Updated on: 03-Nov-2021

782 Views