📌  相关文章
📜  由交替元音和辅音组成的最长子序列

📅  最后修改于: 2021-09-06 06:06:56             🧑  作者: Mango

给定一个非空字符串S ,任务是打印字符串S中包含交替元音和辅音的最长子序列。
注意:如果存在多个具有相同长度的此类子序列,则打印其字符的 ASCII 值之和最大的子序列。
例子:

方法:
请按照以下步骤解决问题:

  • S的第一个字符的 ASCII 值作为当前块的最大值 ( maxi ) 和字符的类型存储在flag 中。如果字符是辅音,则将标志设置为0 ,否则设置为1
  • 遍历字符串的其余部分。
  • 对于每个字符,检查它是否与前一个字符属于同一块。
  • 如果它属于同一个块,则将maxi更新为 max(maxi,i字符的ASCII 值)。
  • 否则,将具有 ASCII 值maxi的字符附加到答案中。将当前i字符的 ASCII 值存储为maxi 。更新标志(标志+ 1)%2来表示当前字符的类型。
  • 遍历整个字符串,将ASCII值为maxi的字符添加到答案中。打印代表子序列的最终字符串。

下面的代码是上述方法的实现:

C++
// C++ program to find the longest
// subsequence containing alternating
// vowels and consonants
#include 
using namespace std;
 
// Function that return 1 or 0
// if the given character is
// vowel or consonant respectively
int isVowel(char c)
{
     
    // Returns 1 if c is vowel
    if (c == 'a' || c == 'e' ||
        c == 'i' || c == 'o' ||
        c == 'u')
        return 1;
 
    // Returns 0 if
    // c is consonant
    return 0;
}
 
// Function to find the longest
// subsequence containing
// alternate vowels and
// consonants
string Subsequence(string str)
{
     
    // Store the length
    // of the string
    int n = str.length();
 
    // Assume first character
    // to be consonant
    int flag = 0;
     
    // If first character is vowel
    if (isVowel(str[0]) == 1)
        flag = 1;
         
    // Store the ASCII value
    int maxi = (int)str[0];
     
    // Stores the final answer
    string ans = "";
    for(int i = 1; i < n; i++)
    {
         
        // If the current character belongs
        // to same category (Vowel / Consonant)
        // as the previous character
        if (isVowel(str[i]) == flag)
        {
             
            // Store the maximum ASCII value
            maxi = max(maxi, (int)str[i]);
        }
         
        // Otherwise
        else
        {
             
            // Store the character with
            // maximum ASCII value from
            // the previous block
            ans += (char)(maxi);
             
            // Store the ASCII of the
            // current character as the
            // maximum of the current block
            maxi = (int)str[i];
             
            // Switch the type of the
            // current block
            flag = (flag + 1) % 2;
        }
    }
     
    // Store the chracter with
    // maximum ASCII value
    // from the last block
    ans += (char)(maxi);
 
    // Return the result
    return ans;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
     
    cout << (Subsequence(str));
}
 
// This code is contributed by chitranayal


Java
// Java program to find the longest
// subsequence containing alternating
// vowels and consonants
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
 
class GFG {
 
    // Function that return 1 or 0
    // if the given character is
    // vowel or consonant respectively
    static int isVowel(char c)
    {
        // Returns 1 if c is vowel
        if (c == 'a' || c == 'e'
            || c == 'i' || c == 'o'
            || c == 'u')
            return 1;
 
        // Returns 0 if
        // c is consonant
        return 0;
    }
 
    // Function to find the longest
    // subsequence containing
    // alternate vowels and
    // consonants
    static String Subsequence(String str)
    {
        // Store the length
        // of the string
        int n = str.length();
 
        // Assume first character
        // to be consonant
        int flag = 0;
        // If first character is vowel
        if (isVowel(str.charAt(0)) == 1)
            flag = 1;
        // Store the ASCII value
        int maxi = (int)str.charAt(0);
        // Stores the final answer
        String ans = "";
        for (int i = 1; i < n; i++) {
            // If the current character belongs
            // to same category (Vowel / Consonant)
            // as the previous character
            if (isVowel(str.charAt(i)) == flag) {
                // Store the maximum ASCII value
                maxi = Math.max(maxi,
                                (int)str.charAt(i));
            }
            // Otherwise
            else {
                // Store the character with
                // maximum ASCII value from
                // the previous block
                ans += (char)(maxi);
                // Store the ASCII of the
                // current character as the
                // maximum of the current block
                maxi = (int)str.charAt(i);
                // Switch the type of the
                // current block
                flag = (flag + 1) % 2;
            }
        }
        // Store the chracter with
        // maximum ASCII value
        // from the last block
        ans += (char)(maxi);
 
        // Return the result
        return ans;
    }
 
    // Driver Program
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        System.out.println(Subsequence(str));
    }
}


Python3
# Python3 program to find the longest
# subsequence containing alternating
# vowels and consonants
 
def isVowel(c):
   
    # boolean function that check whether
    # the given char is vowel or not
    # and returns a boolean value respectively
    vowels=['a','e','i','o','u']
    if(c in vowels):
        return True
    return False
def Subsequence(str):
   
    #string that stores the final result
    ans=''
    flag=(isVowel(str[0]))
     
    #taking the first character
    #as the maximum ASCII valued char
    maxi=ord(str[0])
    for i in range(1,len(str)):
       
        # If the current character belongs to
        # same category(Vowel / Consonant) as the
        # previous character
        if (isVowel(str[i]) == flag):
           
            # choosing a maximum ASCII valued char
            maxi=max(maxi,ord(str[i]))
             
        #otherwise
        else:
            ans=ans+chr(maxi)
            maxi=ord(str[i])
             
            #toggling the flag
            flag=not(flag)
             
    #adding the last char to the answer
    ans=ans+chr(maxi)
    return ans
   
#Driver program
if __name__ == "__main__":
    input_string ='geeksforgeeks'
    print(Subsequence(input_string))
     
# Contributed by
# Nvss Maneesh Gupta


C#
// C# program to find the longest
// subsequence containing alternating
// vowels and consonants
using System;
 
class GFG{
 
// Function that return 1 or 0
// if the given character is
// vowel or consonant respectively
static int isVowel(char c)
{
     
    // Returns 1 if c is vowel
    if (c == 'a' || c == 'e' ||
        c == 'i' || c == 'o' ||
        c == 'u')
        return 1;
 
    // Returns 0 if
    // c is consonant
    return 0;
}
 
// Function to find the longest
// subsequence containing
// alternate vowels and
// consonants
static String Subsequence(String str)
{
     
    // Store the length
    // of the string
    int n = str.Length;
 
    // Assume first character
    // to be consonant
    int flag = 0;
     
    // If first character is vowel
    if (isVowel(str[0]) == 1)
        flag = 1;
     
    // Store the ASCII value
    int maxi = (int)str[0];
     
    // Stores the readonly answer
    String ans = "";
     
    for(int i = 1; i < n; i++)
    {
         
    // If the current character belongs
    // to same category (Vowel / Consonant)
    // as the previous character
    if (isVowel(str[i]) == flag)
    {
             
        // Store the maximum ASCII value
        maxi = Math.Max(maxi, (int)str[i]);
    }
         
    // Otherwise
    else
    {
             
        // Store the character with
        // maximum ASCII value from
        // the previous block
        ans += (char)(maxi);
             
        // Store the ASCII of the
        // current character as the
        // maximum of the current block
        maxi = (int)str[i];
             
        // Switch the type of the
        // current block
        flag = (flag + 1) % 2;
    }
    }
     
    // Store the chracter with
    // maximum ASCII value
    // from the last block
    ans += (char)(maxi);
 
    // Return the result
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "geeksforgeeks";
     
    Console.WriteLine(Subsequence(str));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
gesores

时间复杂度: O(N)

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