Thursday, December 18, 2025
HomeData Modelling & AILongest Subsequence of a String containing only Consonants

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 = “neveropen” 
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 = "neveropen";
     
    // 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 = "neveropen";
     
    // 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 = 'neveropen'
 
# 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 = "neveropen";
     
    // 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 = "neveropen";
 
      // Function call
      document.write(longestConsonantsSubsequence(str) + "<br>");
       
      // This code is contributed by rdtank.
    </script>


Output: 

gksfrgks

 

Time Complexity: O(n)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32455 POSTS0 COMMENTS
Milvus
108 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12037 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS