Given a non-empty string S, the task is to print the longest subsequence from the string S which contains alternating vowels and consonants.
Note: If multiple such subsequences exist having the same length, print the subsequence having the maximum sum of ASCII values of its characters.
Input: S = “geeksforgeeks”
Explanation: “gekorek”, “gekores”, “gekogek”, “gekoges”, “gesorek”, “gesores”, “gesogek”, “gesoges”, “geforek”, “gefores”, “gefogek” and “gefoges” are the possible longest subsequences with alternating consonants and vowels. “gesores” is the subsequence with maximum sum of ASCII values of characters and hence it is the solution.
Input: S = “ababababab”
Explanation: “ababababab” is the longest possible subsequence containing alternating vowels and consonants.
Follow the steps below to solve the problem:
- Store the ASCII value of the first character of S as the maximum of the current block (maxi) and type of the character in flag. If the character is consonant, set flag as 0 and 1 otherwise.
- Traverse through the rest of the string.
- For every character, check if it belongs to the same block as that of the previous character or not.
- If it belongs to same block, update maxi to max(maxi, ASCII value of ith character).
- Otherwise, append the character with ASCII value maxi to the answer. Store the ASCII value of current ith character as maxi. Update flag to (flag + 1)%2 to denote the type of the current character.
- After traversal of entire string, add the character with ASCII value maxi to the answer. Print the final string representing the subsequence.
Below code is the implementation of the above approach:
Time Complexity: O(N)
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Count strings with consonants and vowels at alternate position
- Longest Substring having equal count of Vowels and Consonants
- Longest substring consisting of vowels using Binary Search
- Replace consonants with next immediate consonants alphabetically in a String
- Longest Subsequence of a String containing only Consonants
- Arrange consonants and vowels nodes in a linked list
- Encrypt string with product of number of vowels and consonants in substring of size k
- Check if a string can be converted to another string by replacing vowels and consonants
- Number of words that can be made using exactly P consonants and Q vowels from the given string
- Length of the longest subsequence consisting of distinct elements
- Modify string by removing vowels in between two consonants
- Replace all consonants with nearest vowels in a string
- Modify the string by swapping continuous vowels or consonants
- Check whether all the substrings have number of vowels atleast as that of consonants
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Count of substrings consisting only of vowels
- Count of substrings consisting of even number of vowels
- Length of longest common subsequence containing vowels
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.