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

📅  最后修改于: 2021-09-05 11:47:20             🧑  作者: Mango

给定一个只包含字母的字符串str ,任务是打印只包含元音的字符串str的最长子序列。
例子:

方法:

  • 通过字符的给定字符串的字符遍历。
  • 如果字符是元音,则将其附加到结果字符串。
  • 遍历完成后,所需的仅包含元音的最长子序列存储在结果字符串。

下面是上述方法的实现:

C++
// C++ program to find the longest
// subsequence containing only vowels
 
#include 
using namespace std;
 
// Function to check whether
// a character is vowel or not
bool isVowel(char x)
{
    x = tolower(x);
 
    // Returns true if x is vowel
    return (x == 'a' || x == 'e'
            || x == 'i' || x == 'o'
            || x == 'u');
}
 
// Function to find the longest subsequence
// which contain all vowels
string longestVowelSubsequence(string str)
{
    string answer = "";
 
    // Length of the string
    int n = str.size();
 
    // Iterating through the string
    for (int i = 0; i < n; i++) {
 
        // Checking if the character is a
        // vowel or not
        if (isVowel(str[i])) {
 
            // If it is a vowel, then add it
            // to the final string
            answer += str[i];
        }
    }
 
    return answer;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << longestVowelSubsequence(str)
 << endl;
 
    return 0;
}


Java
// Java program to find the longest
// subsequence containing only vowels
class GFG{
  
// Function to check whether
// a character is vowel or not
static boolean isVowel(char x)
{
    x = Character.toLowerCase(x);
  
    // Returns true if x is vowel
    return (x == 'a' || x == 'e'
            || x == 'i' || x == 'o'
            || x == 'u');
}
  
// Function to find the longest subsequence
// which contain all vowels
static String longestVowelSubsequence(String str)
{
    String answer = "";
  
    // Length of the String
    int n = str.length();
  
    // Iterating through the String
    for (int i = 0; i < n; i++) {
  
        // Checking if the character is a
        // vowel or not
        if (isVowel(str.charAt(i))) {
  
            // If it is a vowel, then add it
            // to the final String
            answer += str.charAt(i);
        }
    }
    return answer;
}
  
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
    System.out.print(longestVowelSubsequence(str)
 +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Python 3
# Python 3 program to find the longest
# subsequence containing only vowels
 
# Function to check whether
# a character is vowel or not
def isVowel(x):
 
    # Returns true if x is vowel
    return (x == 'a' or x == 'e'or x == 'i' or x == 'o' or x == 'u')
     
# Function to find the longest subsequence
# which contain all vowels
def longestVowelSubsequence(str):
     
    answer = ""
 
    # Length of the string
    n = len(str)
 
    # Iterating through the string
    for i in range(n):
 
        # Checking if the character is a
        # vowel or not
        if (isVowel(str[i])):
             
            # If it is a vowel, then add it
            # to the final string
            answer += str[i]
 
    return answer
 
# Driver code
str = "geeksforgeeks"
print(longestVowelSubsequence(str))
 
# This code is contributed by apurva raj


C#
// C# program to find the longest
// subsequence containing only vowels
using System;
 
class GFG{
   
// Function to check whether
// a character is vowel or not
static bool isVowel(char x)
{
    x = char.ToLower(x);
   
    // Returns true if x is vowel
    return (x == 'a' || x == 'e'
            || x == 'i' || x == 'o'
            || x == 'u');
}
   
// Function to find the longest subsequence
// which contain all vowels
static String longestVowelSubsequence(String str)
{
    String answer = "";
   
    // Length of the String
    int n = str.Length;
   
    // Iterating through the String
    for (int i = 0; i < n; i++) {
   
        // Checking if the character is a
        // vowel or not
        if (isVowel(str[i])) {
   
            // If it is a vowel, then add it
            // to the readonly String
            answer += str[i];
        }
    }
    return answer;
}
   
// Driver code
public static void Main(String[] args)
{
    String str = "geeksforgeeks";
    Console.Write(longestVowelSubsequence(str)+"\n");
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
eeoee

时间复杂度: O(N)

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