Reverse substrings of given string according to specified array indices | Function to perform the reversal operation on the given string ; Size of string ; Stores the count of indices ; Count the positions where reversals will begin ; Store the count of reversals beginning at position i ; Check if the count [ i ] is odd the swap the character ; Return the updated string ; Driver Code ; Given str ; Given array of reversing index ; Function Call
def modifyString ( A , str , K ) : NEW_LINE INDENT N = len ( str ) NEW_LINE count = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT count [ A [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , N // 2 + 1 ) : NEW_LINE INDENT count [ i ] = count [ i ] + count [ i - 1 ] NEW_LINE if ( count [ i ] & 1 ) : NEW_LINE INDENT str [ i - 1 ] , str [ N - i ] = str [ N - i ] , str [ i - 1 ] NEW_LINE DEDENT DEDENT return " " . join ( str ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abcdef " NEW_LINE str = [ i for i in str1 ] NEW_LINE arr = [ 1 , 2 , 3 ] NEW_LINE K = len ( arr ) NEW_LINE print ( modifyString ( arr , str , K ) ) NEW_LINE DEDENT