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
Python program to check if the given string is vowel Palindrome
In this article, we will learn how to check if a string becomes a palindrome after removing all consonants, keeping only the vowels.
Problem statement − We are given a string containing both vowels and consonants. We need to remove all consonants and check if the resulting vowel-only string is a palindrome.
A palindrome reads the same forwards and backwards. Our approach involves two steps: first extract only vowels from the string, then check if this vowel string is a palindrome.
Algorithm
The solution follows these steps ?
- Extract all vowels from the input string
- Check if the vowel string is a palindrome by comparing it with its reverse
- Return appropriate result based on the comparison
Complete Implementation
def extract_vowels(s):
"""Extract only vowels from the string"""
vowels = ""
for char in s.lower():
if char in "aeiou":
vowels += char
return vowels
def is_palindrome(s):
"""Check if string is palindrome"""
return s == s[::-1]
def is_vowel_palindrome(s):
"""Check if string becomes palindrome after removing consonants"""
vowel_string = extract_vowels(s)
# If no vowels found
if not vowel_string:
return False, "No vowels found"
# Check if vowel string is palindrome
if is_palindrome(vowel_string):
return True, f"Vowel string '{vowel_string}' is palindrome"
else:
return False, f"Vowel string '{vowel_string}' is not palindrome"
# Test examples
test_strings = ["aeoea", "hello", "racecar", "programming", "aeiuoiea"]
for s in test_strings:
result, message = is_vowel_palindrome(s)
print(f"'{s}': {message}")
'aeoea': Vowel string 'aeoea' is palindrome 'hello': Vowel string 'eo' is not palindrome 'racecar': Vowel string 'aeea' is not palindrome 'programming': Vowel string 'oai' is not palindrome 'aeiuoiea': Vowel string 'aeiuoiea' is palindrome
Step-by-Step Example
Let's trace through the process with the string "aeoea" ?
def trace_vowel_palindrome(s):
print(f"Original string: '{s}'")
# Extract vowels
vowels = ""
for char in s.lower():
if char in "aeiou":
vowels += char
print(f"Found vowel: '{char}', current vowel string: '{vowels}'")
print(f"Final vowel string: '{vowels}'")
print(f"Reversed vowel string: '{vowels[::-1]}'")
print(f"Is palindrome? {vowels == vowels[::-1]}")
# Test with example
trace_vowel_palindrome("aeoea")
Original string: 'aeoea' Found vowel: 'a', current vowel string: 'a' Found vowel: 'e', current vowel string: 'ae' Found vowel: 'o', current vowel string: 'aeo' Found vowel: 'e', current vowel string: 'aeoe' Found vowel: 'a', current vowel string: 'aeoea' Final vowel string: 'aeoea' Reversed vowel string: 'aeoea' Is palindrome? True
Edge Cases
# Test edge cases
edge_cases = ["", "bcdfg", "a", "AeIoU", "xyz"]
for case in edge_cases:
result, message = is_vowel_palindrome(case)
print(f"'{case}': {message}")
'': No vowels found 'bcdfg': No vowels found 'a': Vowel string 'a' is palindrome 'AeIoU': Vowel string 'aeiou' is not palindrome 'xyz': No vowels found
Conclusion
This approach efficiently checks vowel palindromes by first extracting vowels and then verifying palindrome property. The solution handles edge cases like strings with no vowels and works with both uppercase and lowercase letters.
