Python program to check if given string is vowel Palindrome
Last Updated :
21 Mar, 2023
Given a string (may contain both vowel and consonant letters), remove all consonants, then check if the resulting string is palindrome or not. Examples:
Input : abcuhuvmnba
Output : YES
Explanation :
The consonants in the string "abcuhuvmnba"
are removed. Now the string becomes "auua".
Input : xayzuezyax
Output : NO
Input : bkldhgcj
Output : -1
Approach: Remove all the consonants in the string. Check if the vowel string is a palindrome. If it is a palindrome print YES, else print NO. If string contains no vowels, then print -1. Below is the Python implementation:
Python3
# Python program to check if given
# string is vowel Palindrome
# Function to check if a given string is a vowel
def vowel(c):
# creating a list of vowels
v = list("aeiou")
# if the character is a vowel return True
if c in v: return True
return False
# Function to check if a vowel
# string is palindrome
def palindrome(s):
# create a empty list
v = []
# append all vowels into the list
for i in s:
if vowel(i):v.append(i)
# if the length of the vowel
# string is 0 then print -1
if len(v)== 0: print("-1")
# else check if it is a palindrome
else:
# create a reversed string
x = v[::-1]
# initialize a flag
f = 1
for i in range(len(x)):
# if the characters are not the same
if x[i]!= v[i]:
# set the flag to 0
f = 0
break
if f == 1: print("YES")
else: print("NO")
# Driver Code
s = 'abcuhuvmnba'
# calling the main function
palindrome(s.strip())
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach : Using for loop
Python3
# Python program to check if given
# string is vowel Palindrome
s = 'abcuhuvmnba'
vow="aeiou"
x=""
for i in s:
if i in vow:
x+=i
if(len(x)==0):
res=-1
else:
if(x==x[::-1]):
res="YES"
else:
res="NO"
print(res)
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach: Using regular expressions
Python3
import re
def vowel_palindrome(s):
vowels = re.findall(r'[aeiou]', s)
if vowels == vowels[::-1]:
print("YES")
else:
print("NO")
s = 'abcuhuvmnba'
vowel_palindrome(s)
#This code is contributed by vinay pinjala.
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach: Using reversed function:
This is a Python function called vowel_palindrome_2 that takes a single string as input and checks whether the vowels in the string form a palindrome. Here's how the code works:
The function uses the re module in Python to find all the vowels in the input string s using the re.findall() function. The regular expression [aeiou] matches any vowel in the input string, and re.findall() returns a list of all the matches found.
The list of vowels found is reversed using the reversed() function and converted to a regular list using the list() function.
The function then checks if the reversed list of vowels is equal to the original list of vowels. If they are equal, it means that the vowels in the input string form a palindrome. If they are not equal, it means that the vowels do not form a palindrome.
Finally, the function prints "YES" if the vowels form a palindrome and "NO" otherwise.
The function is tested with an example input string 'abcuhuvmnba' using the vowel_palindrome_2() function.
Python3
import re
def vowel_palindrome_2(s):
# Using the re module to find all vowels in the input string `s`
vowels = re.findall(r'[aeiou]', s)
# Reversing the list of vowels found and checking if it is equal to the original list of vowels
if list(reversed(vowels)) == vowels:
print("YES")
else:
print("NO")
# Example input string
s = 'abcuhuvmnba'
# Calling the function
vowel_palindrome_2(s)
#This code is contributed by Jyothi pinjala.
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach 5: Using two pointers to compare characters from both ends
- Initialize two pointers, one pointing to the start of the string and another to the end of the string.
- Traverse the string using the pointers until they meet in the middle:
a. Check if the character at the start pointer is a vowel. If not, move the start pointer to the right.
b. Check if the character at the end pointer is a vowel. If not, move the end pointer to the left.
c. If both characters are vowels, compare them. If they are not equal, print "NO" and exit the function. - If the loop completes without any unequal vowels, print "YES".
Python3
# Python program to check if given
# string is vowel Palindrome
# Function to check if a given character is a vowel
def is_vowel(c):
# creating a set of vowels
vowels = set("aeiou")
# if the character is a vowel return True
return c in vowels
# Function to check if a vowel string is palindrome
def is_vowel_palindrome(s):
# initialize pointers to start and end of the string
start = 0
end = len(s) - 1
# traverse the string using the pointers until they meet in the middle
while start <= end:
# check if the character at the start pointer is a vowel
if not is_vowel(s[start]):
start += 1
continue
# check if the character at the end pointer is a vowel
if not is_vowel(s[end]):
end -= 1
continue
# if both characters are vowels, compare them
if s[start] != s[end]:
print("NO")
return
# move the pointers towards the middle
start += 1
end -= 1
print("YES")
# Driver Code
s = 'abcuhuvmnba'
# calling the main function
is_vowel_palindrome(s.strip())
Time complexity: O(n) where n is the length of the string.
Auxiliary space: O(1)
Similar Reads
Python program to check if given string is pangram
The task is to check if a string is a pangram which means it includes every letter of the English alphabet at least once. In this article, weâll look at different ways to check if the string contains all 26 letters.Using Bitmasking Bitmasking uses a number where each bit represents a letter in the a
2 min read
Python Program to Check if a String is Palindrome or Not
The task of checking if a string is a palindrome in Python involves determining whether a string reads the same forward as it does backward. For example, the string "madam" is a palindrome because it is identical when reversed, whereas "hello" is not. Using two pointer techniqueThis approach involve
3 min read
Python Program To Check String Is Palindrome Using Stack
A palindrome is a sequence of characters that reads the same backward as forward. Checking whether a given string is a palindrome is a common programming task. In this article, we will explore how to implement a Python program to check if a string is a palindrome using a stack. Example Input: str =
3 min read
Python Program To Check If A Singly Linked List Is Palindrome
Given a singly linked list of characters, write a function that returns true if the given list is a palindrome, else false. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. METHOD 1 (Use a Stack): A simple solution is to use a stack of list nodes. This mainly invol
6 min read
Python program to check if a given string is Keyword or not
This article will explore how to check if a given string is a reserved keyword in Python. Using the keyword We can easily determine whether a string conflicts with Python's built-in syntax rules.Using iskeyword()The keyword module in Python provides a function iskeyword() to check if a string is a k
2 min read
Python Program To Check If A Linked List Of Strings Forms A Palindrome
Given a linked list handling string data, check to see whether data is palindrome or not? Examples: Input: a -> bc -> d -> dcb -> a -> NULL Output: True String "abcddcba" is palindrome. Input: a -> bc -> d -> ba -> NULL Output: False String "abcdba" is not palindrome. Reco
2 min read
Python program to check whether the string is Symmetrical or Palindrome
The task of checking whether a string is symmetrical or palindrome in Python involves two main operations . A string is symmetrical if its first half matches the second half, considering the middle character for odd-length strings. A string is palindrome if it reads the same forward and backward.For
4 min read
Python program to check if number is palindrome (one-liner)
In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article. Input1: test_number
3 min read
Python Program to Count the Number of Vowels in a String
In this article, we will be focusing on how to print each word of a sentence along with the number of vowels in each word using Python. Vowels in the English language are: 'a', 'e', 'i', 'o', 'u'. So our task is to calculate how many vowels are present in each word of a sentence. So let us first des
10 min read
Python program to sort Palindrome Words in a Sentence
Given a string S representing a sentence, the task is to reorder all the palindromic words present in the sentence in sorted order. Examples: Input: S = "Please refer to the madam to know the level"Output: Please level to the madam to know the referExplanation: Here "refer", "madam", "level" are the
5 min read