Dada una string str que contiene solo letras, la tarea es imprimir la subsecuencia más larga de la string str que contiene solo vocales.
Ejemplos:
Entrada: str = “geeksforgeeks”
Salida: eeoee
Explicación:
“eeoee” es la subsecuencia más larga de la string que contiene solo vocales.
Entrada: str = “HelloWorld”
Salida: eoo
Explicación:
“eeo” es la subsecuencia más larga de la string que contiene solo vocales.
Acercarse:
- Atraviesa la string dada carácter por carácter.
- Si el carácter es una vocal, agréguelo a la string resultante.
- Cuando se completa el recorrido, la subsecuencia más larga requerida que contiene solo vocales se almacena en la string resultante.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the longest // subsequence containing only vowels #include <bits/stdc++.h> using namespace std; // Function to check whether // a character is vowel or not bool isVowel(char x) { x = tolower(x); // Returns true if x is vowel return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'); } // Function to find the longest subsequence // which contain all vowels string longestVowelSubsequence(string str) { string answer = ""; // Length of the string int n = str.size(); // Iterating through the string for (int i = 0; i < n; i++) { // Checking if the character is a // vowel or not if (isVowel(str[i])) { // If it is a vowel, then add it // to the final string answer += str[i]; } } return answer; } // Driver code int main() { string str = "geeksforgeeks"; cout << longestVowelSubsequence(str) << endl; return 0; }
Java
// Java program to find the longest // subsequence containing only vowels class GFG{ // Function to check whether // a character is vowel or not static boolean isVowel(char x) { x = Character.toLowerCase(x); // Returns true if x is vowel return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'); } // Function to find the longest subsequence // which contain all vowels static String longestVowelSubsequence(String str) { String answer = ""; // Length of the String int n = str.length(); // Iterating through the String for (int i = 0; i < n; i++) { // Checking if the character is a // vowel or not if (isVowel(str.charAt(i))) { // If it is a vowel, then add it // to the final String answer += str.charAt(i); } } return answer; } // Driver code public static void main(String[] args) { String str = "geeksforgeeks"; System.out.print(longestVowelSubsequence(str) +"\n"); } } // This code is contributed by 29AjayKumar
Python 3
# Python 3 program to find the longest # subsequence containing only vowels # Function to check whether # a character is vowel or not def isVowel(x): # Returns true if x is vowel return (x == 'a' or x == 'e'or x == 'i' or x == 'o' or x == 'u') # Function to find the longest subsequence # which contain all vowels def longestVowelSubsequence(str): answer = "" # Length of the string n = len(str) # Iterating through the string for i in range(n): # Checking if the character is a # vowel or not if (isVowel(str[i])): # If it is a vowel, then add it # to the final string answer += str[i] return answer # Driver code str = "geeksforgeeks" print(longestVowelSubsequence(str)) # This code is contributed by apurva raj
C#
// C# program to find the longest // subsequence containing only vowels using System; class GFG{ // Function to check whether // a character is vowel or not static bool isVowel(char x) { x = char.ToLower(x); // Returns true if x is vowel return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'); } // Function to find the longest subsequence // which contain all vowels static String longestVowelSubsequence(String str) { String answer = ""; // Length of the String int n = str.Length; // Iterating through the String for (int i = 0; i < n; i++) { // Checking if the character is a // vowel or not if (isVowel(str[i])) { // If it is a vowel, then add it // to the readonly String answer += str[i]; } } return answer; } // Driver code public static void Main(String[] args) { String str = "geeksforgeeks"; Console.Write(longestVowelSubsequence(str)+"\n"); } } // This code is contributed by Princi Singh
Javascript
<script> // Javascript program to find the longest // subsequence containing only vowels // Function to check whether // a character is vowel or not function isVowel(x) { x = (x.toLowerCase()); // Returns true if x is vowel return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'); } // Function to find the longest subsequence // which contain all vowels function longestVowelSubsequence(str) { var answer = ""; // Length of the string var n = str.length; // Iterating through the string for (var i = 0; i < n; i++) { // Checking if the character is a // vowel or not if (isVowel(str[i])) { // If it is a vowel, then add it // to the final string answer += str[i]; } } return answer; } // Driver code var str = "geeksforgeeks"; document.write( longestVowelSubsequence(str)); // This code is contributed by importantly. </script>
Producción:
eeoee
Complejidad de tiempo: O(N)
Publicación traducida automáticamente
Artículo escrito por muskan_garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA