Python | Alternate vowels and consonants in String
Sometimes, while working with Strings in Python, we can have a problem in which we may need restructure a string, adding alternate vowels and consonants in it. This is a popular school-level problem and having solution to this can be useful. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop + join() + zip_longest()
The combination of above functions can be used to perform this task. In this, we first, separate vowels and consonants in separate lists. And then join alternatively using zip_longest() and join().
Python3
# Python3 code to demonstrate working of # Alternate vowels and consonants in String # using zip_longest() + join() + loop from itertools import zip_longest # initializing string test_str = "gaeifgsbou" # printing original string print ( "The original string is : " + test_str) # Alternate vowels and consonants in String # using zip_longest() + join() + loop vowels = [ 'a' , 'e' , 'i' , 'o' , 'u' ] test_vow = [] test_con = [] for ele in test_str: if ele in vowels: test_vow.append(ele) elif ele not in vowels: test_con.append(ele) res = ' '.join(' '.join(ele) for ele in zip_longest(test_vow, test_con, fillvalue =' ')) # printing result print ( "Alternate consonants vowels are: " + res) |
The original string is : gaeifgsbou Alternate consonants vowels are: agefigosub
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using loop + map() + lambda
This task can also be performed using combination of above functionalities. It is similar as above method, the only differences are usage of map() and lambda functions to perform alternate joining.
Python3
# Python3 code to demonstrate working of # Alternate vowels and consonants in String # using loop + map() + lambda from itertools import zip_longest # initializing string test_str = "gaeifgsbou" # printing original string print ( "The original string is : " + test_str) # Alternate vowels and consonants in String # using loop + map() + lambda vowels = [ 'a' , 'e' , 'i' , 'o' , 'u' ] test_vow = [] test_con = [] for ele in test_str: if ele in vowels: test_vow.append(ele) elif ele not in vowels: test_con.append(ele) res = ''.join( map ( lambda sub: sub[ 0 ] + sub[ 1 ], zip_longest(test_vow, test_con, fillvalue = ''))) # printing result print ( "Alternate consonants vowels are: " + res) |
The original string is : gaeifgsbou Alternate consonants vowels are: agefigosub
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using regular expression:
Python3
import re # Initializing string test_str = "gaeifgsbou" #printing original string print ( "The original string is : " + test_str) # Find all vowels and consonants using regular expressions vowels = re.findall(r '[aeiou]' , test_str) consonants = re.findall(r '[^aeiou]' , test_str) # Zip the lists together and join the result result = ''.join([vowel + consonant for vowel, consonant in zip (vowels, consonants)]) # printing result print ( "Alternate consonants vowels are: " + result) #this code contributed by tvsk |
The original string is : gaeifgsbou Alternate consonants vowels are: agefigosub
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using operator.countOf() method
Python3
# Python3 code to demonstrate working of # Alternate vowels and consonants in String # using operator.countOf() method from itertools import zip_longest import operator as op # initializing string test_str = "gaeifgsbou" # printing original string print ( "The original string is : " + test_str) # Alternate vowels and consonants in String # using operator.countOf() method vowels = 'aeiouAEIOU' test_vow = [] test_con = [] for ele in test_str: if op.countOf(vowels,ele)> 0 : test_vow.append(ele) elif ele not in vowels: test_con.append(ele) res = ' '.join(' '.join(ele) for ele in zip_longest(test_vow, test_con, fillvalue =' ')) # printing result print ( "Alternate consonants vowels are: " + res) |
The original string is : gaeifgsbou Alternate consonants vowels are: agefigosub
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: using for loop and list comprehension
Python3
# initializing string test_str = "gaeifgsbou" # printing original string print ( "The original string is : " + test_str) # Alternate vowels and consonants in String # using two for loops vowels = [ 'a' , 'e' , 'i' , 'o' , 'u' ] vowels_list = [char for char in test_str if char in vowels] consonants_list = [char for char in test_str if char not in vowels] res = '' for i in range ( len (vowels_list)): res + = vowels_list[i] if i < len (consonants_list): res + = consonants_list[i] # printing result print ( "Alternate consonants vowels are: " + res) #This code is contributed by Vinay Pinjala. |
The original string is : gaeifgsbou Alternate consonants vowels are: agefigosub
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using list comprehensions and slicing
Use list comprehensions and slicing to achieve the same result.
Step-by-step approach:
- Initialize two lists, one for vowels and one for consonants.
- Use list comprehension to filter vowels and consonants from the string.
- Use slicing to alternate vowels and consonants.
- Use join() to combine the alternated string.
- Return the alternated string.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of # Alternate vowels and consonants in String # using list comprehension and slicing # initializing string test_str = "gaeifgsbou" # printing original string print ( "The original string is : " + test_str) # Alternate vowels and consonants in String # using list comprehension and slicing vowels = [ 'a' , 'e' , 'i' , 'o' , 'u' ] test_vow = [ele for ele in test_str if ele in vowels] test_con = [ele for ele in test_str if ele not in vowels] res = ''.join([test_vow[i / / 2 ] if i % 2 = = 0 else test_con[i / / 2 ] for i in range ( len (test_str))]) # printing result print ( "Alternate consonants vowels are: " + res) |
The original string is : gaeifgsbou Alternate consonants vowels are: agefigosub
Time complexity: O(n)
Auxiliary space: O(n)
Please Login to comment...