Friday, September 27, 2024
Google search engine
HomeData Modelling & AILength of the longest substring that does not contain any vowel

Length of the longest substring that does not contain any vowel

Given a string S consisting of N lowercase characters, the task is to find the length of the longest substring that does not contain any vowel.

Examples:

Input: S = “neveropen”
Output: 3
The substring “ksf” is the longest substring that does not contain any vowel. The length of this substring is 3.

Input: S = “ceebbaceeffo”
Output: 2

Naive Approach: The simplest approach to solve the given problem is to generate all substrings of the given string S and print the length of the substring of maximum length that does not contain any vowel. 

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized using Sliding Window Technique
Follow the steps below to solve the problem: 

  • Initialize two variables, say count and res as 0, to store the length of string without any vowel, and the maximum length of resultant substring found respectively.
  • Traverse the given string S using the variable i and perform the following steps:
  • After completing the above steps, print the value of res as the result.

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the
// character is a vowel or not
bool vowel(char ch)
{
    if (ch == 'a' || ch == 'e'
        || ch == 'i' || ch == 'o'
        || ch == 'u' || ch == 'A'
        || ch == 'E' || ch == 'I'
        || ch == 'O' || ch == 'U') {
        return true;
    }
    return false;
}
 
// Function to find the length of
// the longest substring that
// doesn't contain any vowel
int maxLengthString(string s)
{
    // Stores the length of
    // the longest substring
    int maximum = 0;
 
    int count = 0;
 
    // Traverse the string, S
    for (int i = 0; i < s.length(); i++) {
 
        // If the current character
        // is vowel, set count as 0
        if (vowel(s[i])) {
            count = 0;
        }
 
        // If the current
        // character is a consonant
        else {
 
            // Increment count by 1
            count++;
        }
 
        // Update the maximum length
        maximum = max(maximum, count);
    }
 
    // Return the result
    return maximum;
}
 
// Driver Code
int main()
{
    string S = "neveropen";
    cout << maxLengthString(S);
 
    return 0;
}


Java




// Java program for the above approach
 
import java.io.*;
 
class GFG {
    public static boolean vowel(char ch)
    {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
            || ch == 'u' || ch == 'A' || ch == 'E'
            || ch == 'I' || ch == 'O' || ch == 'U') {
            return true;
        }
        return false;
    }
 
    // Function to find the length of
    // the longest substring that
    // doesn't contain any vowel
    public static int maxLengthString(String s)
    {
        // Stores the length of
        // the longest substring
        int maximum = 0;
 
        int count = 0;
 
        // Traverse the string, S
        for (int i = 0; i < s.length(); i++) {
 
            // If the current character
            // is vowel, set count as 0
            if (vowel(s.charAt(i))) {
                count = 0;
            }
 
            // If the current
            // character is a consonant
            else {
 
                // Increment count by 1
                count++;
            }
 
            // Update the maximum length
            maximum = Math.max(maximum, count);
        }
 
        // Return the result
        return maximum;
    }
 
    public static void main(String[] args)
    {
        String S = "neveropen";
        System.out.println(maxLengthString(S));
      // This code is contributed by Potta Lokesh
    }


Python




# Python program for the above approach
# Function to check if the
# character is a vowel or not
def vowel(ch):
 
    if (ch == 'a' or ch == 'e'
        or ch == 'i' or ch == 'o'
        or ch == 'u' or ch == 'A'
        or ch == 'E' or ch == 'I'
        or ch == 'O' or ch == 'U'):
        return True
     
        return False
 
# Function to find the length of
# the longest substring that
# doesn't contain any vowel
def maxLengthString(s):
   
    # Stores the length of
    # the longest substring
    maximum = 0  
    count = 0;
     
    # Traverse the string, S
    for i in range(len(s)):
         
        # If the current character
        # is vowel, set count as 0
        if (vowel(s[i])):
            count = 0;
         
        # If the current
        # character is a consonant
        else:
            # Increment count by 1
            count += 1
             
        # Update the maximum length
        maximum = max(maximum, count)
         
    # Return the result
    return maximum
 
# Driver Code
S = 'neveropen'
print(maxLengthString(S))
 
# This code is contributed by shivanisinghss2110


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to check if the
// character is a vowel or not
static bool vowel(char ch)
{
    if (ch == 'a' || ch == 'e' ||
        ch == 'i' || ch == 'o' ||
        ch == 'u' || ch == 'A' ||
        ch == 'E' || ch == 'I' ||
        ch == 'O' || ch == 'U')
    {
        return true;
    }
    return false;
}
 
// Function to find the length of
// the longest substring that
// doesn't contain any vowel
static int maxLengthString(string s)
{
     
    // Stores the length of
    // the longest substring
    int maximum = 0;
 
    int count = 0;
 
    // Traverse the string, S
    for(int i = 0; i < s.Length; i++)
    {
         
        // If the current character
        // is vowel, set count as 0
        if (vowel(s[i]) == true)
        {
            count = 0;
        }
 
        // If the current
        // character is a consonant
        else
        {
             
            // Increment count by 1
            count++;
        }
 
        // Update the maximum length
        maximum = Math.Max(maximum, count);
    }
 
    // Return the result
    return maximum;
}
 
// Driver Code
public static void Main()
{
    string S = "neveropen";
     
    Console.Write(maxLengthString(S));
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript




<script>
 
        // JavaScript program for the above approach
 
        // Function to check if the
        // character is a vowel or not
        function vowel(ch) {
            if (ch == 'a' || ch == 'e'
                || ch == 'i' || ch == 'o'
                || ch == 'u' || ch == 'A'
                || ch == 'E' || ch == 'I'
                || ch == 'O' || ch == 'U') {
                return true;
            }
            return false;
        }
 
        // Function to find the length of
        // the longest substring that
        // doesn't contain any vowel
        function maxLengthString(s) {
            // Stores the length of
            // the longest substring
            let maximum = 0;
 
            let count = 0;
 
            // Traverse the string, S
            for (let i = 0; i < s.length; i++) {
 
                // If the current character
                // is vowel, set count as 0
                if (vowel(s[i])) {
                    count = 0;
                }
 
                // If the current
                // character is a consonant
                else {
 
                    // Increment count by 1
                    count++;
                }
 
                // Update the maximum length
                maximum = Math.max(maximum, count);
            }
 
            // Return the result
            return maximum;
        }
 
        // Driver Code
 
        var S = "neveropen";
        document.write(maxLengthString(S));
 
// This code is contributed by Potta Lokesh
 
</script>


Output: 

3

 

Time Complexity: O(N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments