Given a String List of length N, the following program returns a concatenated list of all its string elements with its alternate elements reversed.
Input: test_str = ‘neveropenneveropen best forneveropen’
Output: skeegskeeg best rofneveropen
Explanation: Alternate words reversed.Input: test_str = ‘neveropenneveropen bestneveropen’
Output: skeegskeeg best skeeg
Explanation: Alternate words reversed.
Method 1: Using reversed() and loop
In this, perform the task of reversing strings using reversed() and then check for alternates using the % operator and concatenate accordingly.
Python3
# initializing string test_str = 'neveropenneveropen is best forneveropen' # printing original string print ( "The original string is : " + str (test_str)) # splitting string temp = test_str.split() res = [] for idx in range ( len (temp)): # reversing if alternate if idx % 2 = = 0 : res.append(''.join( list ( reversed (temp[idx])))) else : res.append(temp[idx]) res = ' ' .join(res) # printing result print ( "Transformed String : " + str (res)) |
The original string is :neveropenneveropen is best forneveropen Transformed String : skeegskeeg is tseb for skeeg
Time Complexity: O(N) , a single for loop takes O(N) time complexity
Auxiliary Space: O(N)
Method 2 : Using slicing and list comprehension
In this, we perform task of reversal using slicing and then list comprehension is used to perform the task done by loop, in shorthand.
Python3
# initializing string test_str = 'neveropenneveropen is best forneveropen' # printing original string print ( "The original string is : " + str (test_str)) # splitting string temp = test_str.split() # list comprehension to solve problem in 1 liner res = ' ' .join([''.join( list ( reversed (temp[idx]))) if idx % 2 = = 0 else temp[idx] for idx in range ( len (temp))]) # printing result print ( "Transformed String : " + str (res)) |
The original string is :neveropenneveropen is best forneveropen Transformed String : skeegskeeg is tseb for skeeg
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 3: Using list comprehension and enumerate
Algorithm for reversing alternate strings and then concatenating all elements using list comprehension and enumerate approach:
Input:
A string test_str
Output:
A transformed string res, which reverses alternate strings and then concatenates all elements
Steps:
Split the given string test_str into a list of words using the split() function, and assign the resulting list to a temporary variable temp.
Iterate over the list of words using enumerate() and list comprehension.
For each word, check if its index (obtained from enumerate) is even or odd. If it’s even, reverse the word using the reversed() function and join the characters to form a new string.
If the index is odd, simply keep the word as it is.
Join the resulting list of words using the join() function, with a space character as the separator.
Assign the resulting string to a variable res and return it as the final output.
Python3
# initializing string test_str = 'neveropenneveropen is best forneveropen' # printing original string print ( "The original string is : " + str (test_str)) # splitting string temp = test_str.split() # using enumerate and list comprehension to solve problem res = ' ' .join([(''.join( list ( reversed (word))) if idx % 2 = = 0 else word) for idx, word in enumerate (temp)]) # printing result print ( "Transformed String : " + str (res)) |
The original string is :neveropenneveropen is best forneveropen Transformed String : skeegskeeg is tseb for skeeg
Time Complexity:
The algorithm uses a single loop and a list comprehension to iterate over the list of words and reverse alternate strings. Thus, the time complexity of the algorithm is O(n), where n is the number of words in the input string.
Auxiliary Space Complexity:
The algorithm uses two variables: temp to store the list of words and res to store the transformed string. The space complexity of these variables is O(n) since they depend on the size of the input string. However, the list comprehension uses temporary variables for each reversed string, so the space complexity is also proportional to the length of the longest string in the input. Therefore, the auxiliary space complexity of the algorithm is O(n + m), where n is the number of words in the input string and m is the length of the longest word in the input.
Method 4: Using map function and lambda function:
Algorithm:
1.Initialize the input string test_str with the value ‘neveropenneveropen is best forneveropen’
2.Split the input string into a list of individual words using whitespace as the delimiter and store it in the words variable.
3.Use the map function along with a lambda function to transform each element of the list.
4.If the index is even, reverse the characters of the corresponding word and return it.
5.If the index is odd, return the word as is.
6.Join the resulting list of transformed words into a string using a space as the separator and store it in the res variable.
7.Print the transformed string.
Python3
# Initialize the string to be transformed test_str = 'neveropenneveropen is best forneveropen' # printing original string print ( "The original string is : " + str (test_str)) # Split the string into individual words using whitespace as the delimiter words = test_str.split() # Use map to apply a function to each element of the range object and join the resulting sequence into a string # The lambda function takes an index i and checks whether it is even. If it is, it reverses the characters of the corresponding word and returns it; otherwise, it returns the word as-is. res = ' ' .join( map ( lambda i: ''.join( list ( reversed (words[i]))) if i % 2 = = 0 else words[i], range ( len (words)))) # Print the transformed string print (res) #This code is contributed by Jyothi pinjala. |
The original string is :neveropenneveropen is best forneveropen skeegskeeg is tseb for skeeg
Time complexity:
The time complexity of the given code is O(nm), where n is the number of words in the input string and m is the length of the longest word. This is because the code needs to iterate over each word in the input string and reverse the characters of every other word. The join function also takes O(n) time to concatenate the transformed words back into a string.
Auxiliary Space:
The space complexity of the given code is O(nm), where n is the number of words in the input string and m is the length of the longest word. This is because the code needs to store each word as a separate string in the list words, and also needs to create a new reversed string for every other word in the list. The resulting transformed string also takes O(nm) space to store.