![]() |
VOOZH | about |
Given a String, perform split on vowels.
Example:
Input : test_str = 'GFGaBst'
Output : ['GFG', 'Bst']
Explanation : a is vowel and split happens on that.Input : test_str = 'GFGaBstuforigeeks'
Output : ['GFG', 'Bst', 'f', 'r', 'g', 'ks']
Explanation : a, e, o, u, i are vowels and split happens on that.
Naive approach:
['GFG', 'Bst', 'f', 'r', 'g', 'ks']
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 1 : Using regex() + split()
In this, we use regex split() which accepts multiple characters to perform split, passing list of vowels, performs split operation over string.
The original string is : GFGaBste4oCS The splitted string : ['GFG', 'Bst', '4', 'CS']
Time Complexity: O(n), where n is the length of the string "test_str". The "re.split" function splits the string by searching for specified characters (vowels), which takes linear time proportional to the length of the string.
Auxiliary space: O(1), as it uses a constant amount of memory regardless of the size of the input string "test_str".
Method 2 : Using replace() and split().
First replace all vowels in string with "*" and then split the string by "*" as delimiter
The original string is : GFGaBste4oCS The splitted string : ['GFG', 'Bst', '4', 'CS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using replace(),split() and ord() methods
The original string is : GFGaBste4oCS The splitted string : ['GFG', 'Bst', '4', 'CS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using operator.countOf() method
['GFG', 'Bst', 'f', 'r', 'g', 'ks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method5# :using the 'itertools.groupby 'function from the 'itertools' module
The original string is: GFGaBste4oCS The split string is: ['GFG', 'Bst', '4', 'CS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using translate method
Algorithm:
The splitted string : ['GFG', 'Bst', '4', 'CS']
Time complexity: The time complexity of this code is O(n) because the maketrans(), translate() and split() methods take linear time.
Auxiliary Space: The space complexity of this code is O(n) because we are creating a new string and a new list to store the results.