Longest substring consisting of vowels using Binary Search

Given a string str of length N, the task is to find the longest substring which contains only vowels using Binary Search technique.
Examples: 
 

Input: str = “baeicba” 
Output:
Explanation: 
Longest substring which contains vowels only is “aei”.
Input: str = “aeiou” 
Output:
 

 

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: 
 

  1. Apply binary search on the lengths ranging from 1 to N.
  2. For each mid-value check if there exists a substring of length mid consisting only of vowels in that substring.
  3. 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.
  4. 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.
  5. Repeat the above three steps until l is less than or equal to r.
  6. Return the max length obtained finally.

Below is the implementation of the above approach:
 



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a character
// is vowel or not
bool vowel(int vo)
{
    // 0-a 1-b 2-c and so on 25-z
    if (vo == 0 || vo == 4
        || vo == 8 || vo == 14
        || vo == 20)
        return true;
    else
        return false;
}
 
// Function to check if any
// substring of length k exists
// which contains only vowels
bool check(string s, int k)
{
    vector<int> cnt(26, 0);
    for (int i = 0; i < k - 1; i++) {
        cnt[s[i] - 'a']++;
    }
 
    // Applying sliding window to get
    // all substrings of length K
    for (int i = k - 1; i < s.size();
         i++) {
        cnt[s[i] - 'a']++;
        int flag1 = 0;
        for (int j = 0; j < 26; j++) {
            if (vowel(j) == false
                && cnt[j] > 0) {
                flag1 = 1;
                break;
            }
        }
        if (flag1 == 0)
            return true;
 
        // Remove the occurence of
        // (i-k+1)th character
        cnt[s[i - k + 1] - 'a']--;
    }
 
    return false;
}
 
// Function to perform  Binary Search
int longestSubstring(string s)
{
    int l = 1, r = s.size();
    int maxi = 0;
 
    // Doing binary search on the lengths
    while (l <= r) {
        int mid = (l + r) / 2;
        if (check(s, mid)) {
            l = mid + 1;
            maxi = max(maxi, mid);
        }
        else
            r = mid - 1;
    }
    return maxi;
}
 
// Driver Code
int main()
{
    string s = "sedrewaefhoiu";
    cout << longestSubstring(s);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Function to check if a character
// is vowel or not
static boolean vowel(int vo)
{
    // 0-a 1-b 2-c and so on 25-z
    if (vo == 0 || vo == 4 ||
        vo == 8 || vo == 14 ||
        vo == 20)
        return true;
    else
        return false;
}
 
// Function to check if any
// subString of length k exists
// which contains only vowels
static boolean check(String s, int k)
{
    int []cnt = new int[26];
    for (int i = 0; i < k - 1; i++)
    {
        cnt[s.charAt(i) - 'a']++;
    }
 
    // Applying sliding window to get
    // all subStrings of length K
    for (int i = k - 1; i < s.length(); i++)
    {
        cnt[s.charAt(i) - 'a']++;
        int flag1 = 0;
        for (int j = 0; j < 26; j++)
        {
            if (vowel(j) == false && cnt[j] > 0)
            {
                flag1 = 1;
                break;
            }
        }
        if (flag1 == 0)
            return true;
 
        // Remove the occurence of
        // (i-k+1)th character
        cnt[s.charAt(i - k + 1) - 'a']--;
    }
 
    return false;
}
 
// Function to perform Binary Search
static int longestSubString(String s)
{
    int l = 1, r = s.length();
    int maxi = 0;
 
    // Doing binary search on the lengths
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (check(s, mid))
        {
            l = mid + 1;
            maxi = Math.max(maxi, mid);
        }
        else
            r = mid - 1;
    }
    return maxi;
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "sedrewaefhoiu";
    System.out.print(longestSubString(s));
}
}
 
// This code is contributed by sapnasingh4991

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of
// the above approach
using System;
class GFG{
  
// Function to check if a character
// is vowel or not
static bool vowel(int vo)
{
    // 0-a 1-b 2-c and so on 25-z
    if (vo == 0 || vo == 4 ||
        vo == 8 || vo == 14 ||
        vo == 20)
        return true;
    else
        return false;
}
  
// Function to check if any
// subString of length k exists
// which contains only vowels
static bool check(String s, int k)
{
    int []cnt = new int[26];
    for (int i = 0; i < k - 1; i++)
    {
        cnt[s[i] - 'a']++;
    }
  
    // Applying sliding window to get
    // all subStrings of length K
    for (int i = k - 1; i < s.Length; i++)
    {
        cnt[s[i] - 'a']++;
        int flag1 = 0;
        for (int j = 0; j < 26; j++)
        {
            if (vowel(j) == false && cnt[j] > 0)
            {
                flag1 = 1;
                break;
            }
        }
        if (flag1 == 0)
            return true;
  
        // Remove the occurence of
        // (i-k+1)th character
        cnt[s[i - k + 1] - 'a']--;
    }
  
    return false;
}
  
// Function to perform Binary Search
static int longestSubString(String s)
{
    int l = 1, r = s.Length;
    int maxi = 0;
  
    // Doing binary search on the lengths
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (check(s, mid))
        {
            l = mid + 1;
            maxi = Math.Max(maxi, mid);
        }
        else
            r = mid - 1;
    }
    return maxi;
}
  
// Driver Code
public static void Main(String[] args)
{
    String s = "sedrewaefhoiu";
    Console.Write(longestSubString(s));
}
}
 
// This code is contributed by sapnasingh4991

chevron_right


Output: 

3

 

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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