📜  最长子串的长度仅由非递增顺序的元音组成

📅  最后修改于: 2021-10-27 08:14:36             🧑  作者: Mango

给定一个由小写字母组成的大小为N的字符串S ,任务是打印仅由按非递增顺序排序的元音组成的最长子字符串的长度。

例子:

方法:使用HashSet可以解决给定的问题。请按照以下步骤解决问题:

  • 以降序将所有元音存储在一个数组中,比如ch[]
  • 将三个变量res、j、 count初始化为0 ,分别存储所需子串的最长长度,迭代所有元音,分别存储当前满足条件的子串的长度。
  • 此外,初始化一个 HashSet 说mp来存储满足条件的当前子字符串的所有不同字符。
  • 使用变量i迭代字符串S的字符并执行以下操作:
    • 如果S[i]等于ch[j]则将S[i]添加到mp然后将jcount增加1 。如果mp的大小等于5 ,则将res更新为rescount的最大值。
    • 否则,如果j + 1小于5并且S[i]等于ch[j + 1] ,则将jcount增加1并将S[i]添加到mp 。如果mp的大小等于5 ,则将res更新为rescount的最大值。
    • 否则,如果S[i]等于 ‘ u ‘,则将0分配给j并将1分配给计数并将S[i]添加到mp 。否则,将0分配给jcount
  • 完成上述步骤后,打印res的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find length of the
// longest substring consisting only
// of vowels in non-increasing order
int count(string S, int N)
{
 
    // Stores all vowels in decreasing order
    char ch[] = { 'u', 'o', 'i', 'e', 'a' };
 
    // Stores current index of array ch[]
    int j = 0;
 
    // Stores the result
    int res = 0;
 
    // Stores the count
    // of current substring
    int count = 0;
 
    // Declare a HashSet to store the vowels
    unordered_set mp;
 
    // Traverse the string, S
    for(int i = 0; i < N; ++i)
    {
         
        // If S[i] is equal to ch[j]
        if (S[i] == ch[j])
        {
             
            // Increment count by 1
            ++count;
 
            // Add S[i] in the mp
            mp.insert(S[i]);
 
            // If length of mp is 5, update res
            if (mp.size() == 5)
            {
                res = max(res, count);
            }
        }
 
        // Else if j+1 is less than 5 and
        // S[i] is equal to ch[j+1]
        else if (j + 1 < 5 && S[i] == ch[j + 1])
        {
             
            // Add the S[i] in the mp
            mp.insert(S[i]);
 
            // Increment count by 1
            ++count;
 
            // Increment j by 1
            j++;
 
            // If length of mp is 5, update res
            if (mp.size() == 5)
            {
                res = max(res, count);
            }
        }
        else
        {
             
            // Clear the mp
            mp.clear();
 
            // If S[i] is 'u'
            if (S[i] == 'u')
            {
                 
                // Add S[i] in the mp
                mp.insert('u');
 
                // Update j and assign 1 to count
                j = 0;
                count = 1;
            }
 
            // Else assign 0 to j and count
            else
            {
                j = 0;
                count = 0;
            }
        }
    }
 
    // Return the result
    return res;
}
 
// Driver Code
int main()
{
    string S = "ueiaoaeiouuoiea";
    int N = S.size();
 
    // Function Call
    cout << count(S, N);
 
    return 0;
}
 
// This code is contributed by Kingash


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find length of the
    // longest substring consisting only
    // of vowels in non-increasing order
    static int count(String S, int N)
    {
 
        // Stores all vowels in decreasing order
        char ch[] = { 'u', 'o', 'i', 'e', 'a' };
 
        // Stores current index of array ch[]
        int j = 0;
 
        // Stores the result
        int res = 0;
 
        // Stores the count
        // of current substring
        int count = 0;
 
        // Declare a HashSet to store the vowels
        HashSet mp = new HashSet<>();
 
        // Traverse the string, S
        for (int i = 0; i < N; ++i) {
 
            // If S[i] is equal to ch[j]
            if (S.charAt(i) == ch[j]) {
 
                // Increment count by 1
                ++count;
 
                // Add S[i] in the mp
                mp.add(S.charAt(i));
 
                // If length of mp is 5, update res
                if (mp.size() == 5) {
                    res = Math.max(res, count);
                }
            }
 
            // Else if j+1 is less than 5 and
            // S[i] is equal to ch[j+1]
            else if (j + 1 < 5
                     && S.charAt(i) == ch[j + 1]) {
 
                // Add the S[i] in the mp
                mp.add(S.charAt(i));
 
                // Increment count by 1
                ++count;
 
                // Increment j by 1
                j++;
 
                // If length of mp is 5, update res
                if (mp.size() == 5) {
                    res = Math.max(res, count);
                }
            }
            else {
 
                // Clear the mp
                mp.clear();
 
                // If S[i] is 'u'
                if (S.charAt(i) == 'u') {
 
                    // Add S[i] in the mp
                    mp.add('u');
 
                    // Update j and assign 1 to count
                    j = 0;
                    count = 1;
                }
 
                // Else assign 0 to j and count
                else {
                    j = 0;
                    count = 0;
                }
            }
        }
 
        // Return the result
        return res;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given Input
        String S = "ueiaoaeiouuoiea";
        int N = S.length();
 
        // Function Call
        System.out.println(count(S, N));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find length of the
# longest substring consisting only
# of vowels in non-increasing order
def count1(S, N):
 
    # Stores all vowels in decreasing order
    ch = ['u', 'o', 'i', 'e', 'a']
 
    # Stores current index of array ch[]
    j = 0
 
    # Stores the result
    res = 0
 
    # Stores the count
    # of current substring
    count = 0;
 
    # Declare a HashSet to store the vowels
    mp = set()
 
    # Traverse the string, S
    for i in range(N):
         
        # If S[i] is equal to ch[j]
        if (S[i] == ch[j]):
             
            # Increment count by 1
            count += 1
 
            # Add S[i] in the mp
            mp.add(S[i])
 
            # If length of mp is 5, update res
            if (len(mp) == 5):
                res = max(res, count)
 
        # Else if j+1 is less than 5 and
        # S[i] is equal to ch[j+1]
        elif (j + 1 < 5 and S[i] == ch[j + 1]):
             
            # Add the S[i] in the mp
            mp.add(S[i])
 
            # Increment count by 1
            count += 1
 
            # Increment j by 1
            j += 1
 
            # If length of mp is 5, update res
            if (len(mp) == 5):
                res = max(res, count)
        else:
             
            # Clear the mp
            mp.clear()
 
            # If S[i] is 'u'
            if (S[i] == 'u'):
                 
                # Add S[i] in the mp
                mp.add('u')
 
                # Update j and assign 1 to count
                j = 0
                count = 1
 
            # Else assign 0 to j and count
            else:
                j = 0
                count = 0
 
    # Return the result
    return res
 
# Driver Code
if __name__ == '__main__':
     
    S = "ueiaoaeiouuoiea"
    N = len(S)
 
    # Function Call
    print(count1(S, N))
     
# This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
6

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程