📌  相关文章
📜  仅包含辅音的字符串的最长子序列

📅  最后修改于: 2021-09-04 07:48:45             🧑  作者: Mango

给定一个仅包含英文字母的字符串,任务是打印仅包含辅音的最长子序列。
例子:

方法 :

  • 首先,我们将遍历给定的字符串。
  • 我们将遍历过程中遇到的所有辅音都包含在答案字符串。
  • 一旦遇到整个初始字符串,我们就有了最长的子序列,只包含辅音。

下面是上述方法的实现:

C++
// C++ program to find the longest subsequence
// which contain all consonants
#include 
 
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


输出:
gksfrgks

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live