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 the characters of the string such that no two adjacent characters are consecutive English alphabets
- 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
- C program to swap adjacent characters of a String
- 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
- Remove first adjacent pairs of similar characters until possible
- Count of strings where adjacent characters are of difference one
- Rearrange numbers in an array such that no two adjacent numbers are same
- Rearrange given string to maximize the occurrence of string t
- Check whether two strings can be made equal by copying their characters with the adjacent ones
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.