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++
#include <bits/stdc++.h>
using namespace std;
bool isConsonants( char x)
{
x = tolower (x);
return !(x == 'a' || x == 'e' || x == 'i'
|| x == 'o' || x == 'u' );
}
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;
}
int main()
{
string str = "geeksforgeeks" ;
cout << longestConsonantsSubsequence(str) << endl;
return 0;
}
|
Java
class GFG{
static boolean isConsonants( char x)
{
x = Character.toLowerCase(x);
return !(x == 'a' || x == 'e' ||
x == 'i' || x == 'o' ||
x == 'u' );
}
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;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
System.out.print(longestConsonantsSubsequence(str) + "\n" );
}
}
|
Python3
def isComsomamts(x):
x = x.lower()
return not (x = = 'a' or x = = 'e' or
x = = 'i' or x = = 'o' or
x = = 'u' )
def longestConsonantsSubsequence(s):
answer = ''
n = len (s)
for i in range (n):
if isComsomamts(s[i]):
answer + = s[i]
return answer
s = 'geeksforgeeks'
print (longestConsonantsSubsequence(s))
|
C#
using System;
class GFG{
static bool isConsonants( char x)
{
x = Char.ToLower(x);
return !(x == 'a' || x == 'e' ||
x == 'i' || x == 'o' ||
x == 'u' );
}
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;
}
static void Main()
{
string str = "geeksforgeeks" ;
Console.Write(longestConsonantsSubsequence(str) + "\n" );
}
}
|
Javascript
<script>
function isConsonants(x) {
x = x.toLowerCase();
return !(x === "a" || x === "e" || x === "i" || x === "o" || x === "u" );
}
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;
}
var str = "geeksforgeeks" ;
document.write(longestConsonantsSubsequence(str) + "<br>" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
30 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...