Python program to check if given string is vowel Palindrome
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()) |
YES
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) |
YES
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. |
YES
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. |
YES
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()) |
YES
Time complexity: O(n) where n is the length of the string.
Auxiliary space: O(1)