Dada una string s de letras minúsculas, necesitamos encontrar la longitud de substring más larga que contenga (a, e, i, o, u) solamente.
Ejemplos:
Input: s = "geeksforgeeks" Output: 2 Longest substring is "ee" Input: s = "theeare" Output: 3
La idea es atravesar la string y realizar un seguimiento del número actual de vocales en la string. Si vemos un carácter que no es una vocal, reiniciamos la cuenta a 0. Pero antes de reiniciar, actualizamos el valor de la cuenta máxima, que será nuestro resultado.
Implementación:
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 max(res, count); } // 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 Math.max(res, count); } // Driver code public static void main (String[] args) { String s = "theeare"; System.out.println(longestVowel(s)); } } // This code is contributed by Mr. Somesh Awasthi
Python3
# Python3 program to find the # longest substring of vowels. 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)): # Increment current count # if s[i] is vowel if (isVowel(s[i])): count += 1 else: # check previous value # is greater then or not res = max(res, count) count = 0 return max(res, count) # Driver code if __name__ == "__main__": s = "theeare" print (longestVowel(s)) # This code is contributed by Chitranayal
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 Math.Max(res, count); } // Driver code public static void Main () { String s = "theeare"; Console.Write(longestVowel(s)); } } // This code is contributed by nitin mittal
PHP
<?php // PHP program to find the // longest substring of vowels. 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++) { // 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 max($res, $count); } // Driver code $s = "theeare"; echo longestVowel($s) ; // This code is contributed // by nitin mittal. ?>
Javascript
<script> // Javascript program to find the // longest substring of vowels. 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++) { // 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 Math.max(res, count); } // Driver code let s = "theeare"; document.write(longestVowel(s)); // This code is contributed by avanitrachhadiya2155 </script>
3
Complejidad temporal: O(n)
Espacio auxiliar: O(1)
Este artículo es una contribución de Ajay Puri . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA