Given a string S consisting of lowercase English letters, the task is to find the length of the longest substring from the given string, having equal number of vowels and consonants.
Input: S = “geeksforgeeks”
Substring “eeksforgee” consists of 5 vowels and 5 consonants. Remaining characters are only consonants. Therefore, any longer substring won’t have equal number of vowels and consonants.
Input: S = “qwertyuiop”
Substring “wertyuio” consists of 4 vowels and 4 consonants.
Naive Approach: The simplest solution is to generate all substrings of the given string and for each substring, check if the count of vowels and consonants are equal or not. Finally, print the maximum length of substring obtained having an equal number of vowels and consonants.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The idea is to consider an array of length equal to that of the given string, storing 1 and -1 corresponding to vowels and consonants respectively and print the length of the longest sub-array with the sum equal to 0 using HashMap.
Below is the implementation of above approach:
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
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
- Encrypt string with product of number of vowels and consonants in substring of size k
- Longest subsequence consisting of alternate vowels and consonants
- Replace consonants with next immediate consonants alphabetically in a String
- Longest substring having K distinct vowels
- Count strings with consonants and vowels at alternate position
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Arrange consonants and vowels nodes in a linked list
- 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
- Find the player to last modify a string such that even number of consonants and no vowels are left in the string
- 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 substring of vowels
- Longest substring consisting of vowels using Binary Search
- Longest substring of vowels with no two adjacent alphabets same
- Longest Subsequence of a String containing only Consonants
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.