


Rearrange the characters in a string so that any two adjacent characters are different, implemented in C++
We are given a string, assuming it is str, the length can be any value. The task is to rearrange the given string so that identical adjacent characters do not line up together in the resulting string.
Let us look at various input and output scenarios:
Input − String str = "itinn"
Output − Rearrange the characters in the string so that two adjacent characters are different. The result is: initn.
Explanation − We are given a variable of string type, assuming it is str. Now we will rearrange the characters of the input string so that no two identical characters appear in the same position, i.e. move 'nn' to a non-adjacent position. So the final result is The string will be 'initn'.
Input − String str = "abbaabbaa"
Output − Rearrange the characters in the string , making adjacent characters different: ababababa
Explanation − We are given a variable of string type, assuming it is str. Now we will rearrange the characters of the input string so that no two same characters appear in the same position i.e. move 'bb', 'aa', 'bb', 'aa' since they are the same and adjacent . So the final string will be ‘ababababa’.
The method used in the following program is as follows
Input a string type variable, assuming it is str, and calculate the size of the string and store it in a name is the length variable.
Check if length is 0, then return.
Pass the data to the function Rearrangement(str, length).
-
Inside function Rearrangement(arr, length)
Set the size of the string to (length 1)/2.
Declare a vector type variable vec(26, 0), which will store integer type data, and a string type pointer ptr(length, ‘ ‘). Also declare a temporary variable temp, of type integer and value 0.
Start looping FOR to iterate str. Inside the loop, set vec[it - ‘a’] .
Create a character type variable ch and set it to the result of calling the maximum(vec) function.
Declare an integer type variable total and set it to vec[ch - ‘a’].
Check if total is greater than size, then return.
Start looping WHILE total, then set ptr[temp] to ch, temp to temp 2, and decrement total by 1.
Set vec[ch - 'a'] to 0. Start looping FOR from i to 0 until i is less than 26. Inside the loop, start the while loop, when vec[i] is greater than 0, set temp to (temp >= length) ? 1: temp, set ptr[temp] to 'a' i, set temp to temp 2, and decrement vec[i] by 1.
Return ptr
-
Inside the function char maximum(const vector
& vec) Declare an integer type variable high and set it to 0, and a character type variable c.
Start looping FOR from i to 0 until i is 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 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, the following output will be generated
Rearrangement of characters in a string such that no two adjacent are same is: initn
The above is the detailed content of Rearrange the characters in a string so that any two adjacent characters are different, implemented in C++. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this article, we will discuss how to modify a given string in C++ by rearranging the vowels alphabetically at their respective indices. We will also explain the methods used to solve this problem and provide examples with test cases. Problem Statement Given a string, rearrange the vowels at their respective indices in alphabetical order. Consonants in the string should maintain their original order. For example, given the string "tutorialspoint", the output should be "tatiriolspount". Method This problem can be solved using a simple algorithm. We can first create a separate string containing all the vowels in the given string in their respective order. We can then sort that string alphabetically. at last,

We have been given two strings and need to check if a permutation of the given strings exists so that one permutation can have larger characters than the other permutation at the i-th index. We can solve the problem by sorting the string and comparing each character in the string one by one. Alternatively, we can use the character frequencies of the two strings to solve the problem. Problem Statement - We are given strings str1 and str2 of length N. We need to check if there are any permutations of strings such that one is lexicographically greater than the other. This means that any permutation should have a character at the i-th index that is greater than the character at the i-th index of another string permutation. Example input -str1="

Manipulating strings is critical in a variety of problem-solving scenarios. The permutation of a given string was found to optimize the number of characters greater than the number of adjacent characters. This is an interesting puzzle that requires rearranging the characters of a string to generate as many pairs of adjacent characters as possible, where the characters on the left are smaller than the characters on the right. . Methods There are several ways to solve permutations of strings where the maximum number of characters is greater than the number of characters directly adjacent to it. Method 1-Backtracking and Pruning-Method 2-Dynamic Programming-Method 3-Heap Algorithm-Method 4-Dictionary Order with Pruning-Method 1: Backtracking and Pruning Use the backtracking algorithm to generate all permutations of the string. At each step, check whether the current arrangement has more characters than its neighbors greater than the maximum found so far. If not, prune early

We are given a string, let's say str, the length can be any value. The task is to rearrange the given string so that identical adjacent characters do not line up together in the resulting string. Let's look at various input and output scenarios: input - string str="itinn" output - rearrange the characters in the string so that two adjacent characters are different, the result is: initn. Explanation −We are given a variable of string type, assuming it is str. Now we will rearrange the characters of the input string such that

We are given a string 'str' of any given length. The task is to rearrange the characters so that the output becomes a palindrome string without adding or removing characters from the given input string. A palindrome string is when the characters are arranged in such a way that they sound the same from start to end. Let’s look at various input and output scenarios for this - Input - String str="itnin" Output - Rearrangement of characters to form a palindrome string if possible is: nitin Explanation - We are given a variable of type String , assumed to be str. Now we will rearrange the characters of the input string so that it becomes

We have an array of positive integer type, let's say arr[], of any size. The task is to rearrange the array such that when we multiply an element with its adjacent elements and then add all the resulting elements, the smallest sum is returned. Let's look at different input and output situations: Input -intarr[]={2,5,1,7,5,0,1,0} Output -Rearrange the array to minimize the sum, which is the product of a consecutive pair of elements Explanation for: 70505121 - We have an array of integers of size 8. Now, we will rearrange the array, which is 70505121. We will check if the minimum sum is returned which is 7*0+5*0+5

How to rearrange the programs in the start menu of win10 system? Many friends may not know much about it. If you don’t know how to rearrange, the editor below has compiled the setting methods for rearranging the programs in the start menu of win10 system. If you are interested. Follow the editor and take a look below! ! How to rearrange the programs in the start menu in win10 system 1. Update the system to version win1010156 to use this function; 2. Click the start menu icon, and then click [All Apps]; 3. Enter all programs in the start menu , and then click the [&] icon at the top, so that you can enter the place where you set the sort order; 4. Choose which initial letter or

In this question, we will execute the given query on the array elements. The query contains a loop of left rotation, right rotation, and update of array elements. The logical part of solving the problem is the array rotation. A simple way to rotate an array left is to replace each element with the next element and the last element with the first element. We can use the deque data structure to rotate the array efficiently. Problem Statement - We are given an arr[] array containing integer values. Additionally, we are given a requests[] array containing K queries. We need to execute each query given in requests[] on the arr[] array elements according to the following rules. {0} - Performs a circular left rotation on an array. {1)-Circle the array
