Given a string, rearrange characters of the given string such that the vowels and consonants occupy alternate position. If string can not be rearranged in desired way, print “no such string”. The order of vowels with respect to each other and the order of consonants with respect to each other should be maintained.
If more than one required strings can be formed, print the lexicographically smaller.
Input : geeks Output : gekes Input : onse Output : nose There are two possible outcomes "nose" and "ones". Since "nose" is lexicographically smaller, we print it.
- Count number of vowels and consonants in given string.
- If difference between counts is more than one, return “Not Possible”.
- If there are more vowels than consonants, print first vowel first and recur for remaining string.
- If there are more consonants than vowels, print first consonant first and recur for remaining string.
- If counts are same, compare first vowel with first consonant and print the smaller one first.
Time Complexity: O(n), where ‘n’ the is length of the string
This article is contributed by Ayush Jauhari. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Lexicographically first alternate vowel and consonant string
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Program to find if a character is vowel or Consonant
- Count of adjacent Vowel Consonant Pairs
- Replace every consonant sequence with its length in the given string
- Check if the given string is vowel prime
- Program to count vowels, consonant, digits and special characters in string.
- Count the nodes of the tree whose weighted string contains a vowel
- Python program to check if given string is vowel Palindrome
- Reverse alternate k characters in a string
- Alternate Lower Upper String Sort
- Rearrange a binary string as alternate x and y occurrences
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Print the string by ignoring alternate occurrences of any character
- Number of flips to make binary string alternate | Set 1