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 = “geeksforgeeks”
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

eeoee

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.