Related Articles

# Length of the longest substring that does not contain any vowel

• Difficulty Level : Basic
• Last Updated : 28 Jun, 2021

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 = “geeksforgeeks”
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 ``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 = ``"geeksforgeeks"``;``    ``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 = ``"geeksforgeeks"``;``        ``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 ``=` `'geeksforgeeks'``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 = ``"geeksforgeeks"``;``    ` `    ``Console.Write(maxLengthString(S));``}``}` `// This code is contributed by SURENDRA_GANGWAR`

## Javascript

 ``
Output:
`3`

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

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up