Longest substring of vowels
Given a string s of lowercase letters, we need to find the longest substring length that contain (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 current number of vowels in the string. If we see a character that is not vowel, we reset count to 0. But before resetting we update max count value which is going to be our result.
C++
// CPP program to find the // longest substring of vowels. #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++) { // Increment current count // if s[i] is vowel if (isVowel(s[i])) count++; else { // check previous value // is greater then or not res = max(res, count); count = 0; } } return res; } // Driver code int main() { string s = "theeare"; cout << longestVowel(s) << endl; return 0; } |
Java
// Java program to find the // longest substring of vowels. 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++) { // Increment current count // if s[i] is vowel if (isVowel(s[i])) count++; else { // check previous value // is greater then or not res = Math.max(res, count); count = 0; } } return res; } // Driver code public static void main (String[] args) { String s = "theeare"; System.out.println(longestVowel(s)); } } // This code is contributed by Mr. Somesh Awasthi |
C#
// C# program to find the // longest substring of vowels. 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++) { // Increment current count // if s[i] is vowel if (isVowel(s[i])) count++; else { // check previous value // is greater then or not res = Math.Max(res, count); count = 0; } } return res; } // Driver code public static void Main () { String s = "theeare"; Console.Write(longestVowel(s)); } } // This code is contributed by nitin mittal |
Output:
3
This article is contributed by Ajay Puri. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Longest substring having K distinct vowels
- Encrypt string with product of number of vowels and consonants in substring of size k
- Longest Ordered Subsequence of Vowels
- Length of longest common subsequence containing vowels
- Longest Common Substring | DP-29
- Longest substring with count of 1s more than 0s
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 1
- Longest Palindromic Substring | Set 2
- Length of the longest valid substring
- Longest repeating and non-overlapping substring
- Longest substring such that no three consecutive characters are same
- Print the longest common substring
- Length of the longest substring with equal 1s and 0s
- Longest Even Length Substring such that Sum of First and Second Half is same
Improved By : nitin mittal



