Python | Reversed Split Strings
Last Updated :
27 Apr, 2023
The functionality of splitting is quite popular with manyfold applications and usages. With all, comes the scopes of many types of variations. This article discusses one among those variations in which one desires to get the split and reversal of element string order, both the operations at once. Let’s discuss certain ways to solve this particular problem.
Method #1 : Using join() + reversed() + split() In this particular method, we first get the element words using the split function, perform the reversal of order of them using the reversed function and then perform the join to bind together the elements.
Python3
test_string = "Gfg is best"
print ("The original string : " + str (test_string))
res = ", ".join( reversed (test_string.split(" ")))
print ("The string after reverse split : " + str (res))
|
Output :
The original string : Gfg is best
The string after reverse split : best, is, Gfg
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using join() + reversed() + split() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using join() + split() + list slicing This method is similar to the above method in which we perform split and join, but the only difference in this method is that we use list slicing to perform the reversal.
Python3
test_string = "Gfg is best"
print ("The original string : " + str (test_string))
res = ', ' .join(test_string.split()[:: - 1 ])
print ("The string after reverse split : " + str (res))
|
Output :
The original string : Gfg is best
The string after reverse split : best, is, Gfg
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using join() + reverse slicing + split() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as additional space res other than the input list itself with the same size of input list.
Method #3:Using List Comprehension
Approach
splitting the original string into a list of words using the split function, then using a list comprehension to reverse the order of the words in the list. Finally, the join function is used to join the reversed list of words with a comma separator to create the new string. This approach has a time complexity of O(n), where n is the length of the original string, and a space complexity of O(n), where n is the length of the original string.
Algorithm
1. Use the split function to split the original string into a list of strings based on whitespace delimiter.
2. Using a list comprehension, reverse the order of the list of strings.
3. Use the join function to join the reversed list of strings with a comma separator.
4. Return the new string.
Python3
def reverse_split_2(original_string):
words = original_string.split()
words = [words[i] for i in range ( len (words) - 1 , - 1 , - 1 )]
new_string = ', ' .join(words)
return new_string
original_string = "Gfg is best"
print (reverse_split_2(original_string))
|
Time complexity: O(n), where n is the length of the original string.
Auxiliary Space: O(n), where n is the length of the original string.
Method #4: Using reduce():
- Import the reduce function from the functools module.
- Define a string variable test_string.
- Use the reduce() function to reverse the order of the words in test_string. The split() method is used to split the string into a list of words, which is then reversed using the slicing notation [::-1]. The lambda function passed to reduce() concatenates the current word with the reversed string so far, separated by a space.
- Use the join() method to join the reversed list of words with a comma separator.
- Print the original string and the reversed string.
Python3
from functools import reduce
test_string = "Gfg is best"
res = reduce ( lambda x, y: y + " " + x, test_string.split()[:: - 1 ])
res = ", " .join( reversed (test_string.split( " " )))
print ( "The original string : " + test_string)
print ( "The string after reverse split : " + res)
|
Output
The original string : Gfg is best
The string after reverse split : best, is, Gfg
The time complexity :O(n), where n is the length of the string. The join() method also has a time complexity of O(n), where n is the length of the final string. The reduce() function has a time complexity of O(n), where n is the number of words in the string. Therefore, the overall time complexity of the code is O(n).
The space complexity :O(n), where n is the length of the final string. This is because the split() method creates a new list of words, and the join() method creates a new string. However, the reduce() function does not create any additional data structures, so its space complexity is O(1).
Share your thoughts in the comments
Please Login to comment...