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 firstname.lastname@example.org. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Count of N size strings consisting of at least one vowel and one consonant
- 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.
- Replace every vowels with lexicographically next vowel in a String
- Python program to check if given string is vowel Palindrome
- Count the nodes of the tree whose weighted string contains a vowel
- 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
- Number of flips to make binary string alternate | Set 1
- Print the string by ignoring alternate occurrences of any character
- Minimum number of characters to be removed to make a binary string alternate
- Program to accept Strings starting with a Vowel
- Substrings starting with vowel and ending with consonants and vice versa