- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

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

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

- Related Articles
- Program to find longest awesome substring in Python
- Program to find length of longest palindromic substring in Python
- Find the Longest Substring Containing Vowels in Even Counts in C++
- Program to find length of longest consecutively increasing substring in Python
- Program to find longest nice substring using Python
- Program to find length of longest repeating substring in a string in Python
- Program to find length of longest palindromic substring after single rotation in Python
- Program to find length of longest substring with even vowel counts in Python
- Program to find length of longest common substring in C++
- Program to find length of longest substring which contains k distinct characters in Python
- Program to sort all vowels at beginning then the consonants, are in sorted order in Python
- Program to find the length of longest substring which has two distinct elements in Python
- Program to find length of longest substring with character count of at least k in Python
- Longest Palindromic Substring in Python
- Find longest consecutive letter and digit substring in Python