Given a string str with repeated characters, the task is to rearrange the characters in a string such that no two adjacent characters are same. If it is possible then print Yes else print No.
Input: str = “geeksforgeeks”
“egeksforegeks” is one such arrangement.
Input: str = “bbbbb”
Approach: The idea is to store the frequency of each character in an unordered_map and compare maximum frequency of character with the difference of string length and maximum frequency number. If the maximum frequency is less than the difference then it can be arranged otherwise not.
Below is the implementation of the above approach:
- Rearrange characters in a string such that no two adjacent are same
- Permutation of a string with maximum number of characters greater than its adjacent characters
- String with k distinct characters and no same characters adjacent
- Rearrange a string so that all same characters become d distance away
- Rearrange a string so that all same characters become atleast d distance away
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements to make adjacent characters unequal in a ternary string
- Rearrange characters to form palindrome if possible
- Count of strings where adjacent characters are of difference one
- Remove first adjacent pairs of similar characters until possible
- Rearrange numbers in an array such that no two adjacent numbers are same
- Rearrange given string to maximize the occurrence of string t
- Min flips of continuous characters to make all characters same in a string
- Rearrange a string in sorted order followed by the integer sum
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.