- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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){ cout<<"Please enter a valid string"; } string count = Rearrangement(str, length); if(count == ""){ cout<<"Please enter a valid string"; } 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

- Related Questions & Answers
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- Program to form smallest number where no two adjacent digits are same in Python
- Python program to Rearrange a string so that all same characters become d distance away
- Ways to paint stairs with two colors such that two adjacent are not yellow in C++
- Maximum subsequence sum such that no three are consecutive
- Rearrange array such that even positioned are greater than odd in C++
- Python - Make pair from two list such that elements are not same in pairs
- Print n 0s and m 1s such that no two 0s and no three 1s are together in C Program