Reverse the substrings of the given String according to the given Array of indices | Function to reverse a string ; Swap character starting from two corners ; Function to reverse the string with the given array of indices ; Reverse the from 0 to A [ 0 ] ; Reverse the for A [ i ] to A [ i + 1 ] ; Reverse String for A [ n - 1 ] to length ; Driver Code
def reverseStr ( str , l , h ) : NEW_LINE INDENT n = h - l NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT str [ i + l ] , str [ n - i - 1 + l ] = str [ n - i - 1 + l ] , str [ i + l ] NEW_LINE DEDENT DEDENT def reverseString ( s , A , n ) : NEW_LINE INDENT reverseStr ( s , 0 , A [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT reverseStr ( s , A [ i - 1 ] , A [ i ] ) NEW_LINE DEDENT reverseStr ( s , A [ n - 1 ] , len ( s ) ) NEW_LINE DEDENT s = " abcdefgh " NEW_LINE s = [ i for i in s ] NEW_LINE A = [ 2 , 4 , 6 ] NEW_LINE n = len ( A ) NEW_LINE reverseString ( s , A , n ) NEW_LINE print ( " " . join ( s ) ) NEW_LINE