Find substrings that contain all vowels in Python

Sometimes we need to find all substrings within a string that contain all vowels (a, e, i, o, u) at least once and contain no consonants. This is useful for text processing and pattern matching tasks.

Problem Statement

Given a string in lowercase alphabets, we need to find all substrings that:

  • Contain all five vowels at least once
  • Contain no consonants

For example, if the input is "helloworldaeiouaieuonicestring", the output will be substrings like 'aeiou', 'aeioua', 'aeiouai', etc.

Algorithm

We'll use a nested loop approach with these steps:

  • For each starting position i, initialize an empty dictionary to track vowels
  • For each ending position j starting from i, check if the character is a vowel
  • If it's a consonant, break the inner loop
  • If it's a vowel, add it to the dictionary
  • When the dictionary contains all 5 vowels, we found a valid substring

Implementation

def is_vowel(x):
    return x in ['a', 'e', 'i', 'o', 'u']

def get_substrings(s):
    n = len(s)
    result = []
    
    for i in range(n):
        vowel_map = {}
        
        for j in range(i, n):
            if not is_vowel(s[j]):
                break
            
            vowel_map[s[j]] = 1
            
            if len(vowel_map) == 5:
                result.append(s[i:j + 1])
    
    return result

# Test the function
s = "helloworldaeiouaieuonicestring"
substrings = get_substrings(s)

print("Input string:", s)
print("Valid substrings:")
for substring in substrings:
    print(substring)
Input string: helloworldaeiouaieuonicestring
Valid substrings:
aeiou
aeioua
aeiouai
aeiouaie
aeiouaieu
eioua
eiouai
eiouaie
eiouaieu
iouai
iouaie
iouaieu
ouaie
ouaieu
uaieu
aeiou
aeioua
aeiouai
aeiouaie
aeiouaieu

How It Works

The algorithm examines every possible substring by:

  1. Starting point iteration: Loop through each character as a potential start
  2. Vowel tracking: Use a dictionary to track unique vowels encountered
  3. Consonant check: Break immediately when a consonant is found
  4. Complete set detection: When all 5 vowels are found, record the substring

Optimized Version

Here's a more efficient version using a set for vowel tracking:

def find_vowel_substrings(s):
    vowels = set('aeiou')
    result = []
    n = len(s)
    
    for i in range(n):
        if s[i] not in vowels:
            continue
            
        found_vowels = set()
        
        for j in range(i, n):
            if s[j] not in vowels:
                break
                
            found_vowels.add(s[j])
            
            if len(found_vowels) == 5:
                result.append(s[i:j + 1])
    
    return result

# Test with example
text = "aeioubaeiouaieuoxy"
substrings = find_vowel_substrings(text)

for substring in substrings:
    print(f"'{substring}' - length: {len(substring)}")
'aeiou' - length: 5
'aeioubaeiou' - length: 11
'aeioubaeiouai' - length: 13
'aeioubaeiouaie' - length: 14
'aeioubaeiouaieu' - length: 15
'aeiou' - length: 5
'aeioua' - length: 6
'aeiouai' - length: 7
'aeiouaie' - length: 8
'aeiouaieu' - length: 9
'eioua' - length: 5
'eiouai' - length: 6
'eiouaie' - length: 7
'eiouaieu' - length: 8
'iouai' - length: 5
'iouaie' - length: 6
'iouaieu' - length: 7
'ouaie' - length: 5
'ouaieu' - length: 6
'uaieu' - length: 5

Conclusion

This nested loop approach efficiently finds all substrings containing all vowels with no consonants. The algorithm has O(n²) time complexity and works well for moderate-length strings. Using sets instead of dictionaries provides cleaner code for vowel tracking.

Updated on: 2026-03-25T09:25:09+05:30

935 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements