Longest Subsequence of a String containing only Consonants


Given a string containing only english alphabets, the task is to print the longest subsequence containing only consonants.

Examples:

Input : str = “geeksforgeeks”
Output : gksfrgks
The longest subsequence containing consonants will be the string of all the consonants in the given string.
Here, it is given by the string “gksfrgks”.

Input : str = “HelloWorld”
Output : HllWrld

Approach :



  • First, we will traverse through the given string.
  • We will include all the consonants that we encounter during our traversal into the answer string.
  • Once the entire initial string has been encountered, we have the longest subsequence containing only consonants with us.

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
// which contain all consonants
#include <bits/stdc++.h>
  
using namespace std;
  
// Returns true if x is consonants.
bool isConsonants(char x)
{
    // Function to check whether a character is
    // consonants or not
    x = tolower(x);
    return !(x == 'a' || x == 'e' || x == 'i' 
                      || x == 'o' || x == 'u');
}
  
// Function to find the longest subsequence
// which contain all consonants
string longestConsonantsSubsequence(string str)
{
    string answer = "";
    int n = str.size();
  
    for (int i = 0; i < n; i++) {
        if (isConsonants(str[i])) {
            answer += str[i];
        }
    }
  
    return answer;
}
  
// Driver code
int main()
{
    string str = "geeksforgeeks";
      
    // Function call
    cout << longestConsonantsSubsequence(str) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the longest subsequence
// which contain all consonants
class GFG{
  
// Returns true if x is consonants.
static boolean isConsonants(char x)
{
      
    // Function to check whether a character 
    // is consonants or not
    x = Character.toLowerCase(x);
    return !(x == 'a' || x == 'e' ||
             x == 'i' || x == 'o' ||
             x == 'u');
}
  
// Function to find the longest subsequence
// which contain all consonants
static String longestConsonantsSubsequence(String str)
{
    String answer = "";
    int n = str.length();
  
    for (int i = 0; i < n; i++) 
    {
        if (isConsonants(str.charAt(i))) 
        {
            answer += str.charAt(i);
        }
    }
    return answer;
}
  
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
      
    // Function call
    System.out.print(longestConsonantsSubsequence(str) + "\n");
}
}
  
// This code is contributed by Amit Katiyar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the longest subsequence 
# which contain all consonants 
  
# Returns true if x is consonants. 
def isComsomamts(x):
      
    x = x.lower()
    return not (x == 'a' or x == 'e' or 
                x == 'i' or x == 'o' or 
                x == 'u')
              
# Function to find the longest subsequence 
# which contain all consonants 
def longestConsonantsSubsequence(s):
      
    answer = ''
    n = len(s)
      
    for i in range(n):
        if isComsomamts(s[i]):
            answer += s[i]
              
    return answer
  
# Driver code 
s = 'geeksforgeeks'
  
# Function call 
print(longestConsonantsSubsequence(s))
  
# This code is contributed by rutvik_56

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the longest subsequence 
// which contain all consonants 
using System;
  
class GFG{
      
// Returns true if x is consonants. 
static bool isConsonants(char x) 
      
    // Function to check whether a  
    // character is consonants or not 
    x = Char.ToLower(x); 
    return !(x == 'a' || x == 'e' || 
             x == 'i' || x == 'o' || 
             x == 'u'); 
      
// Function to find the longest subsequence 
// which contain all consonants 
static string longestConsonantsSubsequence(string str) 
    string answer = ""
    int n = str.Length; 
      
    for(int i = 0; i < n; i++) 
    
       if (isConsonants(str[i])) 
       
           answer += str[i]; 
       
    
    return answer; 
  
// Driver code
static void Main() 
{
    string str = "geeksforgeeks"
      
    // Function call 
    Console.Write(longestConsonantsSubsequence(str) + "\n"); 
}
}
  
// This code is contributed by divyeshrabadiya07    

chevron_right


Output:

gksfrgks

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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.