Flip the String by either swapping given characters or rotating it horizontally for Q queries | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( string S , int N , vector < vector < int > > Queries , int Q ) { for ( int i = 0 ; i < Q ; i ++ ) { int T = Queries [ i ] [ 0 ] , A = Queries [ i ] [ 1 ] , B = Queries [ i ] [ 2 ] ; A -- ; B -- ; if ( T == 1 ) { swap ( S [ A ] , S [ B ] ) ; } else { for ( int j = 0 ; j < N ; j ++ ) { swap ( S [ j ] , S [ j + N ] ) ; } } } cout << S << endl ; } int main ( ) { string S = " ABCD " ; int N = S . length ( ) / 2 ; vector < vector < int > > Queries = { { 2 , 0 , 0 } , { 1 , 1 , 3 } , { 2 , 0 , 0 } } ; int Q = Queries . size ( ) ; solve ( S , N , Queries , Q ) ; return 0 ; }