Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Longest Subsequence of a String containing only Consonants

  • Last Updated : 07 Jun, 2021

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 
 

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 experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

 



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   

Javascript




<script>
      // JavaScript program to find the longest subsequence
      // which contain all consonants
      // Returns true if x is consonants.
      function isConsonants(x) {
        // Function to check whether a
        // character is consonants or not
        x = x.toLowerCase();
        return !(x === "a" || x === "e" || x === "i" || x === "o" || x === "u");
      }
 
      // Function to find the longest subsequence
      // which contain all consonants
      function longestConsonantsSubsequence(str) {
        var answer = "";
        var n = str.length;
 
        for (var i = 0; i < n; i++) {
          if (isConsonants(str[i])) {
            answer += str[i];
          }
        }
        return answer;
      }
 
      // Driver code
      var str = "geeksforgeeks";
 
      // Function call
      document.write(longestConsonantsSubsequence(str) + "<br>");
       
      // This code is contributed by rdtank.
    </script>
Output: 
gksfrgks

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!