Given a string S, the task is to count the number of positions by which the vowels have to be moved such that all the consonants are placed at the front and all the vowels at the end. The order of consonants and vowels in the new string must be same.
Input: S = “abcdefghi”
The consonants present in the string are b, c, d, f, g and h and the vowels are a, e and i. On rearrangement the final string turns out to be “bcdfghaei” and the order of the consonants and vowels is not changed.
Initially ‘a’ was at index 0 and finally it moved to index 6. No. of positions moved = 6 – 0 = 6.
Initially ‘e’ was at index 4 and finally it moved to index 7. No. of positions moved = 7 – 4 = 3.
Initially ‘i’ was at index 8 and it didn’t change its position. So no. of moves = 0.
Total number of positions moved = 6 + 3 + 0 = 9.
Input: S = “iijedf”
The consonants present in the string are j, d and f and the vowels are i, i and e. On rearrangement the final string turns out to be “jdfiie” and the order of the consonants and vowels is not changed.
‘i’ at index 0 is moved to index 3. No. of positions moved = 3 – 0 = 3.
‘i’ at index 1 is moved to index 4. No. of positions moved = 4 – 1 = 3.
‘e’ at index 3 is moved to index 5. No. of positions moved = 5 – 3 = 2.
Total number of positions moved = 3 + 3 + 2 = 8.
- Create the empty strings vowel and consonant to store the vowels and consonants of the given string.
- Traverse the given string S and if the current character is vowel then append it to the string vowel string else append is to the string consonant string.
- Store the concatenation of the strings consonant and vowel in the ans string.
- Initialize 2 pointers p1 and p2 such that p1 points to the 1st index of S and p2 points to the index where the first vowel appears in the ans string.
- Initialize a counter variable cnt to 0.
- Every time the character at index p1 matches with character at index p2, add the value of p2 – p1 to cnt and increment the values of p1 and p2 by 1.
- Repeat the step 6 for every index till the last index of ans is reached.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N), where N is the length of the string.
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.
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Minimum swaps required to convert one binary string to another
- Minimum number of swaps required to make the string K periodic
- Minimum swaps required to make a binary string alternating
- Minimum swaps required to make a binary string divisible by 2^k
- Check if a string can be converted to another string by replacing vowels and consonants
- Replace all consonants with nearest vowels in a string
- Modify string by removing vowels in between two consonants
- C program to count number of vowels and consonants in a String
- Number of words that can be made using exactly P consonants and Q vowels from the given string
- Modify the string by swapping continuous vowels or consonants
- Encrypt string with product of number of vowels and consonants in substring of size k
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Arrange consonants and vowels nodes in a linked list
- Count strings with consonants and vowels at alternate position
- Check whether all the substrings have number of vowels atleast as that of consonants
- Longest subsequence consisting of alternate vowels and consonants
- Replace consonants with next immediate consonants alphabetically in a String
- Minimum swaps to make two strings equal by swapping only with third string
- Minimum number of swaps to make two binary string equal
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.
Improved By : sapnasingh4991