
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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++
- Rearrange the given string such that all Prime Multiple indexes have Same Character
- Python program to Rearrange a string so that all same characters become d distance away
- 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
- Ways to paint stairs with two colors such that two adjacent are not yellow in C++
- Rearrange array such that even positioned are greater than odd in C++
- Rearrange string so that same character become n distance apart JavaScript
- Ways to paint N paintings such that adjacent paintings don’t have same colors in C++
