Flip the String by either swapping given characters or rotating it horizontally for Q queries | Function to find final string after applying all Queries on it ; Traverse the Queries array ; Convert A , B to zero indexing ; Query of 1 st type ; Swap ath and bth characters ; Query of 2 nd type ; Swap first N characters with last N characters ; Print answer ; Driver code ; Input ; Function call
def solve ( S , N , Queries , Q ) : NEW_LINE INDENT S = list ( S ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT T = Queries [ i ] [ 0 ] NEW_LINE A = Queries [ i ] [ 1 ] NEW_LINE B = Queries [ i ] [ 2 ] NEW_LINE A -= 1 NEW_LINE B -= 1 NEW_LINE if ( T == 1 ) : NEW_LINE INDENT temp = S [ A ] NEW_LINE S [ A ] = S [ B ] NEW_LINE S [ B ] = temp NEW_LINE DEDENT else : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT temp = S [ j ] NEW_LINE S [ j ] = S [ j + N ] NEW_LINE S [ j + N ] = temp NEW_LINE DEDENT DEDENT DEDENT S = ' ' . join ( S ) NEW_LINE print ( S ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " ABCD " NEW_LINE N = len ( S ) // 2 NEW_LINE Queries = [ [ 2 , 0 , 0 ] , [ 1 , 1 , 3 ] , [ 2 , 0 , 0 ] ] NEW_LINE Q = len ( Queries ) NEW_LINE solve ( S , N , Queries , Q ) NEW_LINE DEDENT