![]() |
VOOZH | about |
Given list of Strings, task is to add a space before sequence which begin with capital letters.
Input : test_list = ["gfgBest", "forGeeks", "andComputerScienceStudents"]
Output : ['gfg Best', 'for Geeks', 'and Computer Science Students']
Explanation : Words segregated by Capitals.Input : test_list = ["ComputerScienceStudentsLoveGfg"]
Output : ['Computer Science Students Love Gfg']
Explanation : Words segregated by Capitals.
Method #1 : Using loop + join()
This is one of the ways in which this task can be performed. In this, we perform the task of iterating all the strings and then all the characters before adding space using loop in brute force manner. The isupper() is used to check for capital character.
The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
The time complexity of the code is O(nm), where n is the number of strings in the list and m is the average length of the strings. The reason for this is that for each string in the list, the code needs to iterate through its characters and check if it is an upper case character. This takes O(m) time for each string.
The space complexity is O(nm) because of the use of multiple lists to store intermediate results.
Method #2 : Using regex() + list comprehension
The combination of above functions can also be used to solve this problem. In this we employ regex code to check for upper case letters and perform space addition and joining using list comprehension.
The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
Time Complexity: O(n) where n is the number of elements in the list "test_list". The re.sub() function is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) where n is the number of elements in the list "test_list". The re.sub() function creates a new string for each element in the list and hence consumes O(n) space.
Method #3 : Using isupper() and replace() methods
The combination of above functions can also be used to solve this problem. In this we check for any uppercase character using isupper() function and then add an additional space with the use of replace() function.
The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #4 : Using loop, without any builtin methods
The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #5 : Using add_space():
The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] The space added list of strings: ['gfg Best', 'for Geeks', 'and Computer Science']
Time Complexity : O(N^2)
Auxiliary Space : O(N)
Method 6 : using regular expressions and the re module.
step-by-step approach:
The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
The time complexity of this approach is O(nm), where n is the number of strings in the input list and m is the maximum length of a string in the list.
The auxiliary space complexity is O(m), for storing the output list of spaced strings.
METHOD 7:Using defaultdict
APPROACH:
This program takes an original list of strings as input and adds spaces between the potential words. The output is a modified list of strings.
ALGORITHM:
1.Iterate through each word in the original list
2.For each character in the word, check if it is uppercase
3.If it is uppercase, add a space to the previous character index in a defaultdict
4.Concatenate the values of the defaultdict and append it to the modified list
5.Print the modified list
The modified list: ['gfg Best', 'for Geeks', 'and Computer Science']
Time complexity: O(n*m) where n is the length of the original list and m is the average length of each word in the list.
Space complexity: O(n*m) since a defaultdict is created for each word in the list, and each defaultdict may store multiple characters.