Open In App

Python | Reversed Split Strings

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




# Python3 code to demonstrate
# Reverse string split
# using join() + reversed() + split()
 
# initializing string
test_string = "Gfg is best"
 
# printing original string
print("The original string : " + str(test_string))
 
# using join() + reversed() + split()
# Reverse string split
res =  ", ".join(reversed(test_string.split(" ")))
 
# print result
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




# Python3 code to demonstrate
# Reverse string split
# using join() + split() + list slicing
 
# initializing string
test_string = "Gfg is best"
 
# printing original string
print("The original string : " + str(test_string))
 
# using join() + split() + list slicing
# Reverse string split
res =  ', '.join(test_string.split()[::-1])
 
# print result
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))


Output

best, is, Gfg

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():

  1. Import the reduce function from the functools module.
  2. Define a string variable test_string.
  3. 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.
  4. Use the join() method to join the reversed list of words with a comma separator.
  5. Print the original string and the reversed string.

Python3




from functools import reduce
 
test_string = "Gfg is best"
 
# Reverse string split using reduce()
res = reduce(lambda x, y: y + " " + x, test_string.split()[::-1])
res = ", ".join(reversed(test_string.split(" ")))
 
# Print required output
print("The original string : " + test_string)
print("The string after reverse split : " + res)
#This code is contributed by Pushpa.


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).



Last Updated : 27 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads