Given string str of size N consists of lower-case English alphabets. The task is to find the arrangement of the characters of the string such that no two adjacent characters are neighbors in English alphabets. In case of multiple answers print any of them. If no such arrangement is possible then print -1.
Input: str = “aabcd”
No two adjacent characters are neighbours in English alphabets.
Input: str = “aab”
Approach: Traverse through the string and store all odd positioned characters in a string odd and even positioned characters in another string even i.e. every two consecutive characters in both the strings will have an absolute difference in ASCII values of at least 2. Then sort both the strings. Now, if any of the concatenation (even + odd) or (odd + even) is valid then print the valid arrangement else it is not possible to rearrange the string in the required way.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of character pairs at same distance as in English alphabets
- For each lowercase English alphabet find the count of strings having these alphabets
- Rearrange characters in a string such that no two adjacent are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- Rearrange string such that no pair of adjacent characters are of the same type
- Longest substring of vowels with no two adjacent alphabets same
- Minimum string such that every adjacent character of given string is still adjacent
- Rearrange array such that difference of adjacent elements is in descending order
- Count characters at same position as in English alphabet
- Replace '?' in a string such that no two adjacent characters are same
- Remove consecutive alphabets which are in same case
- Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged
- Remove all characters other than alphabets from string
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Check if frequency of each character is equal to its position in English Alphabet
- Rearrange an array to minimize sum of product of consecutive pair elements
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Rearrange the given string such that all prime multiple indexes have same character
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.