Length of the longest substring consisting only of vowels in non-increasing order
Last Updated :
12 Jan, 2022
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[9], S[15]}, 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++
#include <bits/stdc++.h>
using namespace std;
int count(string S, int N)
{
char ch[] = { 'u' , 'o' , 'i' , 'e' , 'a' };
int j = 0;
int res = 0;
int count = 0;
unordered_set< char > mp;
for ( int i = 0; i < N; ++i)
{
if (S[i] == ch[j])
{
++count;
mp.insert(S[i]);
if (mp.size() == 5)
{
res = max(res, count);
}
}
else if (j + 1 < 5 && S[i] == ch[j + 1])
{
mp.insert(S[i]);
++count;
j++;
if (mp.size() == 5)
{
res = max(res, count);
}
}
else
{
mp.clear();
if (S[i] == 'u' )
{
mp.insert( 'u' );
j = 0;
count = 1;
}
else
{
j = 0;
count = 0;
}
}
}
return res;
}
int main()
{
string S = "ueiaoaeiouuoiea" ;
int N = S.size();
cout << count(S, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int count(String S, int N)
{
char ch[] = { 'u' , 'o' , 'i' , 'e' , 'a' };
int j = 0 ;
int res = 0 ;
int count = 0 ;
HashSet<Character> mp = new HashSet<>();
for ( int i = 0 ; i < N; ++i) {
if (S.charAt(i) == ch[j]) {
++count;
mp.add(S.charAt(i));
if (mp.size() == 5 ) {
res = Math.max(res, count);
}
}
else if (j + 1 < 5
&& S.charAt(i) == ch[j + 1 ]) {
mp.add(S.charAt(i));
++count;
j++;
if (mp.size() == 5 ) {
res = Math.max(res, count);
}
}
else {
mp.clear();
if (S.charAt(i) == 'u' ) {
mp.add( 'u' );
j = 0 ;
count = 1 ;
}
else {
j = 0 ;
count = 0 ;
}
}
}
return res;
}
public static void main(String[] args)
{
String S = "ueiaoaeiouuoiea" ;
int N = S.length();
System.out.println(count(S, N));
}
}
|
Python3
def count1(S, N):
ch = [ 'u' , 'o' , 'i' , 'e' , 'a' ]
j = 0
res = 0
count = 0 ;
mp = set ()
for i in range (N):
if (S[i] = = ch[j]):
count + = 1
mp.add(S[i])
if ( len (mp) = = 5 ):
res = max (res, count)
elif (j + 1 < 5 and S[i] = = ch[j + 1 ]):
mp.add(S[i])
count + = 1
j + = 1
if ( len (mp) = = 5 ):
res = max (res, count)
else :
mp.clear()
if (S[i] = = 'u' ):
mp.add( 'u' )
j = 0
count = 1
else :
j = 0
count = 0
return res
if __name__ = = '__main__' :
S = "ueiaoaeiouuoiea"
N = len (S)
print (count1(S, N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int count( string S, int N)
{
char [] ch = { 'u' , 'o' , 'i' , 'e' , 'a' };
int j = 0;
int res = 0;
int count = 0;
HashSet< char > mp = new HashSet< char >();
for ( int i = 0; i < N; ++i) {
if (S[i] == ch[j]) {
++count;
mp.Add(S[i]);
if (mp.Count == 5) {
res = Math.Max(res, count);
}
}
else if (j + 1 < 5 && S[i] == ch[j + 1]) {
mp.Add(S[i]);
++count;
j++;
if (mp.Count == 5) {
res = Math.Max(res, count);
}
}
else {
mp.Clear();
if (S[i] == 'u' ) {
mp.Add( 'u' );
j = 0;
count = 1;
}
else {
j = 0;
count = 0;
}
}
}
return res;
}
public static void Main( string [] args)
{
string S = "ueiaoaeiouuoiea" ;
int N = S.Length;
Console.WriteLine(count(S, N));
}
}
|
Javascript
<script>
function count(S, N)
{
let ch = [ 'u' , 'o' , 'i' , 'e' , 'a' ];
let j = 0;
let res = 0;
let count = 0;
let mp = new Map();
for (let i = 0; i < N; ++i)
{
if (S[i] == ch[j])
{
++count;
mp.set(S[i], "" );
if (mp.size == 5)
{
res = Math.max(res, count);
}
}
else if (j + 1 < 5 && S[i] == ch[j + 1])
{
mp.set(S[i], "" );
++count;
j++;
if (mp.size == 5)
{
res = Math.max(res, count);
}
}
else
{
mp.clear();
if (S[i] == 'u' )
{
mp.set( 'u' , "" );
j = 0;
count = 1;
}
else
{
j = 0;
count = 0;
}
}
}
return res;
}
let S = "ueiaoaeiouuoiea" ;
let N = S.length;
document.write(count(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...