Skip to content
Related Articles

Related Articles

Improve Article
Longest Subsequence of a String containing only vowels
  • Last Updated : 27 May, 2021

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




// 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));
 
// Thi code is contributed by importantly.
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes 




My Personal Notes arrow_drop_up
Recommended Articles
Page :