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
def vowel(c):
v = list ( "aeiou" )
if c in v: return True
return False
def palindrome(s):
v = []
for i in s:
if vowel(i):v.append(i)
if len (v) = = 0 : print ( "-1" )
else :
x = v[:: - 1 ]
f = 1
for i in range ( len (x)):
if x[i]! = v[i]:
f = 0
break
if f = = 1 : print ( "YES" )
else : print ( "NO" )
s = 'abcuhuvmnba'
palindrome(s.strip())
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach : Using for loop
Python3
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)
|
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):
vowels = re.findall(r '[aeiou]' , s)
if list ( reversed (vowels)) = = vowels:
print ( "YES" )
else :
print ( "NO" )
s = 'abcuhuvmnba'
vowel_palindrome_2(s)
|
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
def is_vowel(c):
vowels = set ( "aeiou" )
return c in vowels
def is_vowel_palindrome(s):
start = 0
end = len (s) - 1
while start < = end:
if not is_vowel(s[start]):
start + = 1
continue
if not is_vowel(s[end]):
end - = 1
continue
if s[start] ! = s[end]:
print ( "NO" )
return
start + = 1
end - = 1
print ( "YES" )
s = 'abcuhuvmnba'
is_vowel_palindrome(s.strip())
|
Time complexity: O(n) where n is the length of the string.
Auxiliary space: O(1)