Python Program to Count characters surrounding vowels
Given a String, the task is to write a Python program to count those characters which have vowels as their neighbors.
Examples:
Input : test_str = ‘geeksforgeeksforgeeks’
Output : 10
Explanation : g, k, f, r, g, k, f, r, g, k have surrounding vowels.
Input : test_str = ‘geeks’
Output : 2
Explanation : g, k have surrounding vowels.
Method 1 : Using loop
In this, we increment counter while checking previous and successive element for vowels using loop.
Python3
test_str = 'geeksforgeeksforgeeks'
print ( "The original string is : " + str (test_str))
res = 0
vow_list = [ 'a' , 'e' , 'i' , 'o' , 'u' ]
for idx in range ( 1 , len (test_str) - 1 ):
if test_str[idx] not in vow_list and (test_str[idx - 1 ] in vow_list or test_str[idx + 1 ] in vow_list):
res + = 1
if test_str[ 0 ] not in vow_list and test_str[ 1 ] in vow_list:
res + = 1
if test_str[ - 1 ] not in vow_list and test_str[ - 2 ] in vow_list:
res + = 1
print ( "Characters around vowels count : " + str (res))
|
Output:
The original string is : geeksforgeeksforgeeks
Characters around vowels count : 10
Method 2 : Using sum() and list comprehension
In this, we perform the task of getting count using sum() and iteration and filtering is done using list comprehension.
Python3
test_str = 'geeksforgeeksforgeeks'
print ( "The original string is : " + str (test_str))
vow_list = [ 'a' , 'e' , 'i' , 'o' , 'u' ]
res = sum ([ 1 for idx in range ( 1 , len (test_str) - 1 ) if test_str[idx]
not in vow_list and (test_str[idx - 1 ] in vow_list or test_str[idx + 1 ] in vow_list)])
if test_str[ 0 ] not in vow_list and test_str[ 1 ] in vow_list:
res + = 1
if test_str[ - 1 ] not in vow_list and test_str[ - 2 ] in vow_list:
res + = 1
print ( "Characters around vowels count : " + str (res))
|
Output:
The original string is : geeksforgeeksforgeeks
Characters around vowels count : 10
The time and space complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Method 3: Using enumerate () method.
Python3
test_str = 'geeksforgeeksforgeeks'
print ( "The original string is : " + str (test_str))
count = 0
for idx, val in enumerate (test_str):
if val in 'aeiouAEIOU' :
if idx > 0 and test_str[idx - 1 ] not in 'aeiouAEIOU' :
count + = 1
if idx < len (test_str) - 1 and test_str[idx + 1 ] not in 'aeiouAEIOU' :
count + = 1
print ( "Characters around vowels count : " + str (count))
|
Output
The original string is : geeksforgeeksforgeeks
Characters around vowels count : 10
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 4: Using Regex
Explanation
In this approach, we use the re.findall() method to find the characters that are surrounded by vowels.
The regular expression r'[^aeiouAEIOU]([aeiouAEIOU])[^aeiouAEIOU]’ matches a character that is not a vowel, followed by a vowel, followed by a character that is not a vowel.
The len() function is used to count the number of matches found.
Python3
import re
test_str = 'geeksforgeeksforgeeks'
print ( "The original string is : " + str (test_str))
res = len (re.findall(r '[^aeiouAEIOU]([aeiouAEIOU])' , test_str)) + len (re.findall(r '([aeiouAEIOU])[^aeiouAEIOU]' , test_str))
print ( "Characters around vowels count : " + str (res))
|
Output
The original string is : geeksforgeeksforgeeks
Characters around vowels count : 10
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...