Related Articles

# Length of the longest substring consisting only of vowels in non-increasing order

• Last Updated : 15 Jun, 2021

Given a string S of size N consisting of lowercase alphabets, the task is to print the length of the longest substring consisting only of vowels sorted in non-increasing order.

Examples:

Input: S = “ueiaoaeiouuoiea”
Output: 6
Explanation: The only substring which consists only of vowels in non-increasing order, is the substring {S, S}, which is “uuoiea”.

Input: S = “uuuioea”
Output: 0

Approach: The given problem can be solved using HashSet. Follow the steps below to solve the problem:

• Store all the vowels in an array, say ch[], in decreasing order.
• Initialize three variables res, j, and count as 0, to store the longest length of the required substring, to iterate over all vowels, and to store the length of the current substring satisfying the conditions respectively.
• Also, initialize a HashSet say mp to store all the distinct characters of the current substring satisfying the conditions.
• Iterate over the characters of the string S using a variable i and perform the following operations:
• If S[i] is equal to ch[j] then add S[i] to mp and then increment j and count by 1. If the size of mp is equal to 5, then update res to a maximum of res and count.
• Otherwise, if j + 1 is less than 5 and S[i] is equal to ch[j + 1], then increment j and count by 1 and add S[i] to mp. If the size of mp is equal to 5, then update res to a maximum of res and count.
• Otherwise, if S[i] is equal to ‘u‘ then assign 0 to j and 1 to count and add S[i] to mp. Otherwise, assign 0 to both j and count.
• After completing the above steps, print the value of res as the result.

Below is the implementation of the above approach:

## 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<``char``> 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

 ``
Output:
`6`

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up