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 = “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++
#include <bits/stdc++.h>
using namespace std;
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 ;
}
int maxLengthString(string s)
{
int maximum = 0;
int count = 0;
for ( int i = 0; i < s.length(); i++) {
if (vowel(s[i])) {
count = 0;
}
else {
count++;
}
maximum = max(maximum, count);
}
return maximum;
}
int main()
{
string S = "geeksforgeeks" ;
cout << maxLengthString(S);
return 0;
}
|
Java
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 ;
}
public static int maxLengthString(String s)
{
int maximum = 0 ;
int count = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if (vowel(s.charAt(i))) {
count = 0 ;
}
else {
count++;
}
maximum = Math.max(maximum, count);
}
return maximum;
}
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
System.out.println(maxLengthString(S));
}
|
Python
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
def maxLengthString(s):
maximum = 0
count = 0 ;
for i in range ( len (s)):
if (vowel(s[i])):
count = 0 ;
else :
count + = 1
maximum = max (maximum, count)
return maximum
S = 'geeksforgeeks'
print (maxLengthString(S))
|
C#
using System;
using System.Collections.Generic;
class GFG{
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 ;
}
static int maxLengthString( string s)
{
int maximum = 0;
int count = 0;
for ( int i = 0; i < s.Length; i++)
{
if (vowel(s[i]) == true )
{
count = 0;
}
else
{
count++;
}
maximum = Math.Max(maximum, count);
}
return maximum;
}
public static void Main()
{
string S = "geeksforgeeks" ;
Console.Write(maxLengthString(S));
}
}
|
Javascript
<script>
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 maxLengthString(s) {
let maximum = 0;
let count = 0;
for (let i = 0; i < s.length; i++) {
if (vowel(s[i])) {
count = 0;
}
else {
count++;
}
maximum = Math.max(maximum, count);
}
return maximum;
}
var S = "geeksforgeeks" ;
document.write(maxLengthString(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...