Given string str of length N, the task is to find the longest substring which contains only vowels using the Binary Search technique.
Input: str = “baeicba”
Longest substring which contains vowels only is “aei”.
Input: str = “aeiou”
Approach: Refer to the Longest substring of vowels for an approach in O(N) complexity.
Binary Search Approach: In this article, we are using a Binary Search based approach:
Follow the steps below to solve the problem:
- Apply binary search on the lengths ranging from 1 to N.
- For each mid-value check if there exists a substring of length mid consisting only of vowels in that substring.
- If there exists a substring of length mid, then update the value of max and update l as mid+1 to check if a substring of length greater than mid exists or not which consists only of vowels.
- If no such substring of length mid exists, update r as mid-1 to check if a substring of length smaller than mid exists or not which consists only of vowels.
- Repeat the above three steps until l is less than or equal to r.
- Return the max length obtained finally.
Below is the implementation of the 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.
- Longest subsequence consisting of alternate vowels and consonants
- Longest substring with K unique characters using Binary Search
- Count of substrings consisting only of vowels
- Count of substrings consisting of even number of vowels
- Longest substring of vowels
- Longest substring having K distinct vowels
- Longest substring of vowels with no two adjacent alphabets same
- Longest Substring having equal count of Vowels and Consonants
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Smallest String consisting of a String S exactly K times as a Substring
- Meta Binary Search | One-Sided Binary Search
- Encrypt string with product of number of vowels and consonants in substring of size k
- Lexicographically smallest K-length substring containing maximum number of vowels
- Length of the smallest substring which contains all vowels
- Longest Common Prefix using Binary Search
- Count of binary strings of given length consisting of at least one 1
- Longest common substring in binary representation of two numbers
- Length of longest common subsequence containing vowels
- Longest Ordered Subsequence of Vowels
- Longest Subsequence of a String containing only 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.