Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find longest substring of all vowels in order in Python
A beautiful substring contains all five vowels (a, e, i, o, u) in alphabetical order, with each vowel appearing at least once. We need to find the longest such substring in a given string.
Problem Definition
A string is beautiful if it satisfies these conditions ?
Each of the 5 vowels must appear at least once in it
Letters must be sorted in alphabetical sequence
For example, in string "aaioaaaaeiiouuooaauu", the substring "aaaaeiiouu" is beautiful and has length 10.
Algorithm
We use a two-pointer sliding window approach ?
Initialize vowels list ['a', 'e', 'i', 'o', 'u']
Use left pointer (l) and right pointer (r) to track current window
For each position, check if we can form a valid beautiful substring
Expand right pointer through each vowel in sequence
Track the maximum length found
Implementation
def find_longest_beautiful_substring(s):
vowels = ['a', 'e', 'i', 'o', 'u']
left, right, longest = 0, 0, 0
while left < len(s):
valid = True
# Check each vowel in order
for vowel in vowels:
# Check if current vowel exists at right pointer
valid &= (right < len(s) and s[right] == vowel)
# Skip all occurrences of current vowel
while right < len(s) and s[right] == vowel:
right += 1
# If we found all vowels in order, update longest
if valid:
longest = max(longest, right - left)
left = right
return longest
# Test the function
s = "aaioaaaaeiiouuooaauu"
result = find_longest_beautiful_substring(s)
print(f"Longest beautiful substring length: {result}")
Longest beautiful substring length: 10
How It Works
Let's trace through the example "aaioaaaaeiiouuooaauu" ?
def trace_algorithm(s):
vowels = ['a', 'e', 'i', 'o', 'u']
left, right, longest = 0, 0, 0
print(f"Input string: {s}")
print("Tracing the algorithm:")
iteration = 1
while left < len(s):
start_left, start_right = left, right
valid = True
for vowel in vowels:
if right < len(s) and s[right] == vowel:
count = 0
while right < len(s) and s[right] == vowel:
count += 1
right += 1
else:
valid = False
break
if valid:
length = right - left
longest = max(longest, length)
print(f"Iteration {iteration}: Found beautiful substring '{s[start_left:right]}' (length: {length})")
left = right
iteration += 1
if iteration > 5: # Prevent too much output
break
return longest
s = "aaioaaaaeiiouuooaauu"
result = trace_algorithm(s)
print(f"\nFinal result: {result}")
Input string: aaioaaaaeiiouuooaauu Tracing the algorithm: Iteration 1: Found beautiful substring 'aaaaeiiouu' (length: 10) Final result: 10
Edge Cases
# Test various edge cases
test_cases = [
"aeiou", # Minimum beautiful string
"aaa", # Only one vowel
"aabbcc", # Invalid characters (but let's assume all vowels)
"uoiea", # Reverse order
"aeeiioouu", # Missing 'a' at start after first
"" # Empty string
]
for test in test_cases:
if all(c in 'aeiou' for c in test): # Only process vowel strings
result = find_longest_beautiful_substring(test)
print(f"'{test}' ? {result}")
'aeiou' ? 5 'aaa' ? 0 'uoiea' ? 0 'aeeiioouu' ? 0 '' ? 0
Conclusion
The algorithm uses a sliding window approach to find beautiful substrings efficiently. It checks each position for the presence of all vowels in alphabetical order, making it optimal for this specific pattern-matching problem.
