Skip to content
Related Articles

Related Articles

Longest Subsequence of a String containing only Consonants
  • Last Updated : 05 Jun, 2020


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




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


Java




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


Python3




# 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


C#




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


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
Recommended Articles
Page :