Longest substring of vowels
Last Updated :
18 Sep, 2023
Given a string s of lowercase letters, we need to find the longest substring length that contains (a, e, i, o, u) only.
Examples :
Input: s = "geeksforgeeks"
Output: 2
Longest substring is "ee"
Input: s = "theeare"
Output: 3
The idea is to traverse the string and keep track of the current number of vowels in the string. If we see a character that is not a vowel, we reset count to 0. But before resetting we update the max count value which is going to be our result.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
int longestVowel(string s)
{
int count = 0, res = 0;
for ( int i = 0; i < s.length(); i++)
{
if (isVowel(s[i]))
count++;
else
{
res = max(res, count);
count = 0;
}
}
return max(res, count);
}
int main()
{
string s = "theeare" ;
cout << longestVowel(s) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
static int longestVowel(String str)
{
int count = 0 , res = 0 ;
char [] s = str.toCharArray();
for ( int i = 0 ; i < s.length; i++)
{
if (isVowel(s[i]))
count++;
else
{
res = Math.max(res, count);
count = 0 ;
}
}
return Math.max(res, count);
}
public static void main (String[] args)
{
String s = "theeare" ;
System.out.println(longestVowel(s));
}
}
|
Python3
def isVowel(c):
return (c = = 'a' or c = = 'e' or
c = = 'i' or c = = 'o' or
c = = 'u' )
def longestVowel(s):
count, res = 0 , 0
for i in range ( len (s)):
if (isVowel(s[i])):
count + = 1
else :
res = max (res, count)
count = 0
return max (res, count)
if __name__ = = "__main__" :
s = "theeare"
print (longestVowel(s))
|
C#
using System;
class GFG
{
static bool isVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
static int longestVowel(String str)
{
int count = 0, res = 0;
char []s = str.ToCharArray();
for ( int i = 0; i < s.Length; i++)
{
if (isVowel(s[i]))
count++;
else
{
res = Math.Max(res, count);
count = 0;
}
}
return Math.Max(res, count);
}
public static void Main ()
{
String s = "theeare" ;
Console.Write(longestVowel(s));
}
}
|
PHP
<?php
function isVowel( $c )
{
return ( $c == 'a' || $c == 'e' ||
$c == 'i' || $c == 'o' ||
$c == 'u' );
}
function longestVowel( $s )
{
$count = 0; $res = 0;
for ( $i = 0; $i < strlen ( $s ); $i ++)
{
if (isVowel( $s [ $i ]))
$count ++;
else
{
$res = max( $res , $count );
$count = 0;
}
}
return max( $res , $count );
}
$s = "theeare" ;
echo longestVowel( $s ) ;
?>
|
Javascript
<script>
function isVowel(c)
{
return (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' );
}
function longestVowel(str)
{
let count = 0, res = 0;
let s = str.split( "" );
for (let i = 0; i < s.length; i++)
{
if (isVowel(s[i]))
count++;
else
{
res = Math.max(res, count);
count = 0;
}
}
return Math.max(res, count);
}
let s = "theeare" ;
document.write(longestVowel(s));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...