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 = ‘zambiatekforzambiatek’
Output : 10
Explanation : g, k, f, r, g, k, f, r, g, k have surrounding vowels.Input : test_str = ‘zambiatek’
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
# initializing stringtest_str = 'zambiatekforzambiatek'# printing original stringprint("The original string is : " + str(test_str))res = 0vow_list = ['a', 'e', 'i', 'o', 'u']for idx in range(1, len(test_str) - 1): # checking for preceding and succeeding element to be vowel 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# solving for 1st and last elementif test_str[0] not in vow_list and test_str[1] in vow_list: res += 1if test_str[-1] not in vow_list and test_str[-2] in vow_list: res += 1# printing resultprint("Characters around vowels count : " + str(res)) |
Output:
The original string is :zambiatekforzambiatek
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
# initializing stringtest_str = 'zambiatekforzambiatek'# printing original stringprint("The original string is : " + str(test_str))vow_list = ['a', 'e', 'i', 'o', 'u']# sum() accumulates all vowels surround elementsres = 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)])# solving for 1st and last elementif test_str[0] not in vow_list and test_str[1] in vow_list: res += 1if test_str[-1] not in vow_list and test_str[-2] in vow_list: res += 1# printing resultprint("Characters around vowels count : " + str(res)) |
Output:
The original string is :zambiatekforzambiatek
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
# initializing stringtest_str = 'zambiatekforzambiatek'# printing original stringprint("The original string is : " + str(test_str))count=0#counting vowel surrounding characters using enumerate() methodfor 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# printing resultprint("Characters around vowels count : " + str(count)) |
The original string is :zambiatekforzambiatek 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
#Importing the regular expression moduleimport re#initializing stringtest_str = 'zambiatekforzambiatek'#printing original stringprint("The original string is : " + str(test_str))#Using re.findall() to find the number of characters surrounded by vowelsres = len(re.findall(r'[^aeiouAEIOU]([aeiouAEIOU])', test_str)) + len(re.findall(r'([aeiouAEIOU])[^aeiouAEIOU]', test_str))#printing resultprint("Characters around vowels count : " + str(res)) |
The original string is :zambiatekforzambiatek Characters around vowels count : 10
Time Complexity: O(n)
Auxiliary Space: O(n)



