Given alphanumeric string str, the task is to rearrange the string such that no two adjacent characters are of the same type, i.e., no two adjacent characters can be alphabets or digits. If no such arrangement is possible, print -1.
Input: str = “geeks2020”
Input: str = “IPL20”
Naive Approach: The simplest approach is to generate all possible permutation of the given string and for every permutation, check if it satisfies the given conditions or not. If fount to be true for any permutation, print that permutation. If no such permutation exists, then print -1.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to store all the alphabets and the digits separately and rearrange them by placing them alternatively in the resultant string. If the count of the alphabets and the digits differ by more than 1, print -1 as no desired arrangement is possible.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
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.
- 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 the characters of the string such that no two adjacent characters are consecutive English alphabets
- String with k distinct characters and no same characters adjacent
- Replace '?' in a string such that no two adjacent characters are same
- Rearrange a string so that all same characters become atleast d distance away
- Rearrange a string so that all same characters become d distance away
- Minimum string such that every adjacent character of given string is still adjacent
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Rearrange the given string such that all prime multiple indexes have same character
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Maximize cost of deletions to obtain string having no pair of similar adjacent characters
- Group consecutive characters of same type in a string
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Construct a graph which does not contain any pair of adjacent nodes with same value
- Min flips of continuous characters to make all characters same in a string
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
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.