# Program to find longest substring of all vowels in order in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a string s with only English vowels, we have to find the length of the longest beautiful substring of s. If we cannot find such substring then, return 0. A string is said to be beautiful if it satisfies the following conditions −

• Each of the 5 vowels must appear at least once in it.

• Letters must be sorted in alphabetical sequence

So, if the input is like s = "aaioaaaaeiiouuooaauu", then the output will be 10 because the substring is "aaaaeiiouu" which is beautiful.

To solve this, we will follow these steps −

• vowels := a list of all vowels ['a', 'e', 'i', 'o', 'u']

• l := 0, r := 0, longest := 0

• while l < size of s, do

• valid := True

• for each vowel in vowels, do

• valid := valid is true and (r < size of s and s[r] is same as vowel)

• while r < size of s and s[r] is same as vowel, do

• r := r + 1

• if valid is true, then

• longest := maximum of longest and (r - l)

• l := r

• return longest

## Example

Let us see the following implementation to get better understanding −

def solve(s):
vowels = ['a', 'e', 'i', 'o', 'u']
l, r, longest = 0, 0, 0
while (l < len(s)):
valid = True
for vowel in vowels:
valid &= (r < len(s) and s[r] == vowel)
while (r < len(s) and s[r] == vowel):
r += 1
if (valid):
longest = max(longest, r - l)
l = r
return longest

s = "aaioaaaaeiiouuooaauu"
print(solve(s))

## Input

"aaioaaaaeiiouuooaauu"


## Output

10
Updated on 07-Oct-2021 13:18:48