Given a String, reverse its alternate characters string.
Input : test_str = ‘Lazyroar4rLazyroar’
Output : keekr4sgeegs
Explanation : only g, e, s, r, e, k are reversed, rest all have same position.Input : test_str = ‘Lazyroar’
Output : egkes
Method #1 : Using loop + slicing + reversed()
This is one of the ways in which this task can be performed. In this, we extract alternates using slicing and then reverse the string using reversed. The reconstruction of the string is done using a loop.
Python3
# Python3 code to demonstrate working of # Alternate characters reverse in String # Using loop + slicing + reversed() # initializing string test_str = 'Lazyroar4rLazyroar' # printing original string print ( "The original string is : " + str (test_str)) # extracting alternate string alt = test_str[:: 2 ] not_alt = test_str[ 1 :: 2 ] # performing reverse alt = "".join( reversed (alt)) res = '' # remaking string for idx in range ( len (alt)): res + = alt[idx] res + = not_alt[idx] # printing result print ( "Is alternate reversed string : " + str (res)) |
The original string is : Lazyroar4rLazyroar Is alternate reversed string : keekr4sgeegs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using list comprehension
This is one more way in which this task can be performed. In this, we perform a similar function as above by using one-liner functionality using list comprehension.
Python3
# Python3 code to demonstrate working of # Alternate characters reverse in String # Using list comprehension # initializing string test_str = 'Lazyroar4rLazyroar' # printing original string print ( "The original string is : " + str (test_str)) # using one-liner to solve the problem res = " ".join([" ".join( reversed (test_str[:: 2 ]))[idx] + test_str[ 1 :: 2 ][idx] for idx in range ( len ("".join( reversed (test_str[:: 2 ]))))]) # printing result print ( "Is alternate reversed string : " + str (res)) |
The original string is : Lazyroar4rLazyroar Is alternate reversed string : keekr4sgeegs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#3: Using join() and zip()
Python3
test_str = 'Lazyroar4rLazyroar' # printing original string print ( "The original string is : " + str (test_str)) # extracting alternate string alt = test_str[:: 2 ] not_alt = test_str[ 1 :: 2 ] # performing reverse alt = "".join( reversed (alt)) # remaking string res = "".join([x + y for x, y in zip (alt, not_alt)]) # printing result print ( "Is alternate reversed string : " + str (res)) #This code is contributed by Vinay pinjala. |
The original string is : Lazyroar4rLazyroar Is alternate reversed string : keekr4sgeegs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#4: Using loop and string concatenation.
Python3
# Python3 code to demonstrate working of # Alternate characters reverse in String # Using loop and string concatenation. def alternate_reverse(string): res = '' for i in range ( len (string)): if i % 2 = = 0 : res = string[i + 1 ] + res else : res = string[i - 1 ] + res return res test_str = 'Lazyroar4rLazyroar' # printing original string print ( "The original string is : " + str (test_str)) res = alternate_reverse(test_str) # printing result print ( "Is alternate reversed string : " + str (res)) #this code contributed by tvsk |
The original string is : Lazyroar4rLazyroar Is alternate reversed string : kseergs4ekge
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5 : Using regular expressions
- Import the re module.
- Define a regular expression pattern that matches alternating groups of two characters in the string.
- Define a function that takes a match object as its argument and returns the reversed version of the match group.
- Use the re.sub() function to replace the matched groups in the string with their reversed version.
- Return the modified string.
Python3
# Python3 code to demonstrate working of # Alternate characters reverse in String # Using regular expressions # import the re module import re # initializing string test_str = 'Lazyroar4rLazyroar' # printing original string print ( "The original string is : " + str (test_str)) # define a regular expression pattern that matches alternating groups of two characters pattern = r '(.)(.)' # define a function to reverse the matched group def reverse_match(match_obj): return match_obj.group( 2 ) + match_obj.group( 1 ) # use re.sub() function to replace the matched groups in the string with their reversed version res = re.sub(pattern, reverse_match, test_str) # printing result print ( "Is alternate reversed string : " + str (res)) |
The original string is : Lazyroar4rLazyroar Is alternate reversed string : egke4sgreesk
Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(n), as we need to create a new string to store the modified string.