Longest Subsequence of a String containing only vowels

Given a string str containing only alphabets, the task is to print the longest subsequence of the string str containing only vowels.
Examples:
Input: str = “zambiatek”
Output: eeoee
Explanation:
“eeoee” is the longest subsequence of the string containing only vowels.
Input: str = “HelloWorld”
Output: eoo
Explanation:
“eeo” is the longest subsequence of the string containing only vowels.
Approach:
- Traverse through the given string character by character.
- If the character is a vowel, append it to the resultant string.
- When the traversal completes, the required longest subsequence containing only vowels is stored in the resultant string.
Below is the implementation of the above approach:
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 notbool 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 vowelsstring 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 codeint main(){ string str = "zambiatek"; cout << longestVowelSubsequence(str) << endl; return 0;} |
Java
// Java program to find the longest// subsequence containing only vowelsclass GFG{ // Function to check whether// a character is vowel or notstatic 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 vowelsstatic 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 codepublic static void main(String[] args){ String str = "zambiatek"; 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 notdef 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 vowelsdef 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 codestr = "zambiatek"print(longestVowelSubsequence(str))# This code is contributed by apurva raj |
C#
// C# program to find the longest// subsequence containing only vowelsusing System;class GFG{ // Function to check whether// a character is vowel or notstatic 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 vowelsstatic 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 codepublic static void Main(String[] args){ String str = "zambiatek"; 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 notfunction 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 vowelsfunction 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 codevar str = "zambiatek";document.write( longestVowelSubsequence(str));// This code is contributed by importantly.</script> |
Output:
eeoee
Time Complexity: O(N)
Auxiliary Space: O(N), The extra space is used to store the result.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



