Given a string str of length N, the task is to find the longest substring which contains only vowels using Binary Search technique.
Input: str = “baeicba”
Longest substring which contains vowels only is “aei”.
Input: str = “aeiou”
Approach: Refer to 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 smallerthan 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 substring with K unique characters using Binary Search
- Longest subsequence consisting of alternate vowels and consonants
- Longest substring of vowels
- Longest substring having K distinct vowels
- Longest substring of vowels with no two adjacent alphabets same
- Count of substrings consisting only of vowels
- Longest common substring in binary representation of two numbers
- Count of substrings consisting of even number of vowels
- Longest Common Prefix using Binary Search
- Length of the smallest substring which contains all vowels
- Lexicographically smallest K-length substring containing maximum number of vowels
- Encrypt string with product of number of vowels and consonants in substring of size k
- Longest Subsequence of a String containing only vowels
- Longest Ordered Subsequence of Vowels
- Length of longest common subsequence containing vowels
- Count of binary strings of given length consisting of at least one 1
- Meta Binary Search | One-Sided Binary Search
- Recursive function to do substring search
- Why is Binary Search preferred over Ternary Search?
- Longest Common Substring | DP-29
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.
Improved By : sapnasingh4991