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.
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.
- Lexicographically first alternate vowel and consonant string
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Count of N size strings consisting of at least one vowel and one consonant
- Count of adjacent Vowel Consonant Pairs
- Program to find if a character is vowel or Consonant
- Program to count vowels, consonant, digits and special characters in string.
- Replace every consonant sequence with its length in the given string
- Python program to check if given string is vowel Palindrome
- Check if the given string is vowel prime
- Replace every vowels with lexicographically next vowel in a String
- Minimize cost to replace all the vowels of a given String by a single vowel
- Count the nodes of the tree whose weighted string contains a vowel
- Substrings starting with vowel and ending with consonants and vice versa
- Arrangement of words without changing the relative position of vowel and consonants
- Program to accept Strings starting with a Vowel
- Count pair of strings whose concatenation has every vowel
- 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
- Minimum number of characters to be removed to make a binary string alternate
- Print the string by ignoring alternate occurrences of any character