Flip the String by either swapping given characters or rotating it horizontally for Q queries
Given a string S of length 2N and Q Queries containing three integers T, A, and B each, where queries can be of the following two types:
- T=1: Swap the Ath and Bth characters in S.(In 1-based indexing)
- T=2: Swap the first N characters with the last N characters i.e “ABCD” becomes “CDAB”
The task is to find the final string after applying the Q Queries to it.
Examples:
Input: S=”ABCD”, N=2, Q={{2, 0, 0}, {1, 1, 3}, {2, 0, 0}}
Output:
CBAD
Explanation:
After 1st query: S=”CDAB”
After 2nd query: S=”ADCB”
After 3rd query: S=”CBAD”
Input: S=”GEEK”, N=2, Q={{2, 0, 0}, {1, 1, 2}, {1, 2, 3}, {1, 3, 4}, {2, 0, 0}}
Output:
EEKG
Naive Approach: Follow the steps below to solve the problem:
- Traverse the Queries array, and for each current index i, do the following:
- Extract T, A and B as T=Q[i][0], A=Q[i][1] and B=Q[i][2].
- Decrement A and B both to make them 0-indexed.
- If T is equal to 1, swap the characters at index A and B respectively.
- Otherwise, Traverse the string from 0 to N-1 and swap each A[j] with A[j+N].
- Print the string S
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class Main {
static void solve(String S, int N,
List<List<Integer> > Queries, int Q)
{
for ( int i = 0 ; i < Q; i++) {
int T = Queries.get(i).get( 0 ),
A = Queries.get(i).get( 1 ),
B = Queries.get(i).get( 2 );
A--;
B--;
if (T == 1 ) {
char [] sArr = S.toCharArray();
char temp = sArr[A];
sArr[A] = sArr[B];
sArr[B] = temp;
S = new String(sArr);
}
else {
char [] sArr = S.toCharArray();
for ( int j = 0 ; j < N; j++) {
char temp = sArr[j];
sArr[j] = sArr[j + N];
sArr[j + N] = temp;
}
S = new String(sArr);
}
}
System.out.println(S);
}
public static void main(String[] args)
{
String S = "ABCD" ;
int N = S.length() / 2 ;
List<List<Integer> > Queries = Arrays.asList(
Arrays.asList( 2 , 0 , 0 ), Arrays.asList( 1 , 1 , 3 ),
Arrays.asList( 2 , 0 , 0 ));
int Q = Queries.size();
solve(S, N, Queries, Q);
}
}
|
Python3
def solve(S, N, Queries, Q):
S = list (S)
for i in range (Q):
T = Queries[i][ 0 ]
A = Queries[i][ 1 ]
B = Queries[i][ 2 ]
A - = 1
B - = 1
if (T = = 1 ):
temp = S[A]
S[A] = S[B]
S[B] = temp
else :
for j in range (N):
temp = S[j]
S[j] = S[j + N]
S[j + N] = temp
S = ''.join(S)
print (S)
if __name__ = = '__main__' :
S = "ABCD"
N = len (S) / / 2
Queries = [ [ 2 , 0 , 0 ],
[ 1 , 1 , 3 ],
[ 2 , 0 , 0 ] ]
Q = len (Queries)
solve(S, N, Queries, Q)
|
C#
using System;
using System.Collections.Generic;
class Solution {
static void Solve( string S, int N,
List<List< 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) {
char [] charArray = S.ToCharArray();
char temp = charArray[A];
charArray[A] = charArray[B];
charArray[B] = temp;
S = new string (charArray);
}
else {
char [] charArray = S.ToCharArray();
for ( int j = 0; j < N; j++) {
char temp = charArray[j];
charArray[j] = charArray[j + N];
charArray[j + N] = temp;
}
S = new string (charArray);
}
}
Console.WriteLine(S);
}
static void Main() {
string S = "ABCD" ;
int N = S.Length / 2;
List<List< int >> Queries = new List<List< int >> {
new List< int > { 2, 0, 0 },
new List< int > { 1, 1, 3 },
new List< int > { 2, 0, 0 }
};
int Q = Queries.Count;
Solve(S, N, Queries, Q);
}
}
|
Javascript
function solve(S, N, Queries, Q) {
let arr = S.split( '' );
for (let i = 0; i < Q; i++) {
let T = Queries[i][0];
let A = Queries[i][1];
let B = Queries[i][2];
A -= 1;
B -= 1;
if (T === 1) {
let temp = arr[A];
arr[A] = arr[B];
arr[B] = temp;
} else {
for (let j = 0; j < N; j++) {
let temp = arr[j];
arr[j] = arr[j + N];
arr[j + N] = temp;
}
}
}
S = arr.join( '' );
console.log(S);
}
let S = "ABCD" ;
let N = Math.floor(S.length / 2);
let Queries = [ [ 2, 0, 0 ],
[ 1, 1, 3 ],
[ 2, 0, 0 ] ];
let Q = Queries.length;
solve(S, N, Queries, Q);
|
Time Complexity: O(N*Q)
Auxiliary Space: O(1)
Efficient Approach: There is no need to actually swap first N characters with last N characters for every query of type 2. This can be done once at the end by keeping track of how many times this is done in a separate variable. Follow the steps below to solve the problem:
- Initialize a variable flip to 0, which keeps track of how many times a query of type 2 is performed on S.
- Traverse the Queries array, and for each current index i, do the following:
- Extract T, A and B as T=Q[i][0], A=Q[i][1] and B=Q[i][2].
- Decrement A and B both to make them 0-indexed.
- If T is equal to 1, do the following:
- Check if flip is even. If it is, simply swap the Ath and Bth characters in S
- Otherwise, update the values of A and B accordingly (as the string is flipped )by adding N to them and then, taking their modulus with 2N. Then swap the Ath and Bth characters in S.
- Otherwise, increment flip
- Check if flip is even. If it is, print S as it is.
- Otherwise, S is flipped, so print the last N characters of S first and then, the first N characters of S.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve(string S, int N,
vector<vector< int > > Queries,
int Q)
{
int flip = 0;
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) {
if (flip % 2 == 0)
swap(S[A], S[B]);
else {
A = (A + N) % (2 * N);
B = (B + N) % (2 * N);
swap(S[A], S[B]);
}
}
else {
flip++;
}
}
if (flip % 2 == 0)
cout << S << endl;
else {
for ( int i = N; i < 2 * N; i++)
cout << S[i];
for ( int i = 0; i < N; i++)
cout << S[i];
cout << 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;
}
|
Java
import java.util.*;
class Main {
static void solve(String S, int N,
List<List<Integer> > Queries, int Q)
{
int flip = 0 ;
for ( int i = 0 ; i < Q; i++) {
int T = Queries.get(i).get( 0 ),
A = Queries.get(i).get( 1 ),
B = Queries.get(i).get( 2 );
A--;
B--;
if (T == 1 ) {
if (flip % 2 == 0 )
S = swap(S, A, B);
else {
A = (A + N) % ( 2 * N);
B = (B + N) % ( 2 * N);
S = swap(S, A, B);
}
}
else {
flip++;
}
}
if (flip % 2 == 0 )
System.out.println(S);
else {
for ( int i = N; i < 2 * N; i++)
System.out.print(S.charAt(i));
for ( int i = 0 ; i < N; i++)
System.out.print(S.charAt(i));
System.out.println();
}
}
static String swap(String S, int i, int j)
{
char [] arr = S.toCharArray();
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return new String(arr);
}
public static void main(String[] args)
{
String S = "ABCD" ;
int N = S.length() / 2 ;
List<List<Integer> > Queries = Arrays.asList(
Arrays.asList( 2 , 0 , 0 ), Arrays.asList( 1 , 1 , 3 ),
Arrays.asList( 2 , 0 , 0 ));
int Q = Queries.size();
solve(S, N, Queries, Q);
}
}
|
Python3
def solve(st, N, Queries, Q):
flip = 0
for i in range (Q):
T, A, B = Queries[i]
A - = 1
B - = 1
if T = = 1 :
if flip % 2 = = 0 :
st[A], st[B] = st[B], st[A]
else :
A = (A + N) % ( 2 * N)
B = (B + N) % ( 2 * N)
st[A], st[B] = st[B], st[A]
else :
flip + = 1
if flip % 2 = = 0 :
print (''.join(st))
else :
ans = []
for i in range (N, 2 * N):
ans.append(S[i])
for i in range (N):
ans.append(S[i])
print (''.join(ans))
if __name__ = = '__main__' :
str = "ABCD"
N = len ( str ) / / 2
Queries = [[ 2 , 0 , 0 ], [ 1 , 1 , 3 ], [ 2 , 0 , 0 ]]
Q = len (Queries)
solve( list ( str ), N, Queries, Q)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class MainClass {
static void Solve( string S, int N, List<List< int >> Queries, int Q) {
int flip = 0;
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) {
if (flip % 2 == 0)
S = Swap(S, A, B);
else {
A = (A + N) % (2 * N);
B = (B + N) % (2 * N);
S = Swap(S, A, B);
}
}
else {
flip++;
}
}
if (flip % 2 == 0)
Console.WriteLine(S);
else {
for ( int i = N; i < 2 * N; i++)
Console.Write(S[i]);
for ( int i = 0; i < N; i++)
Console.Write(S[i]);
Console.WriteLine();
}
}
static string Swap( string S, int i, int j) {
char [] arr = S.ToCharArray();
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return new string (arr);
}
public static void Main() {
string S = "ABCD" ;
int N = S.Length / 2;
List<List< int >> Queries = new List<List< int >>() {
new List< int > {2, 0, 0},
new List< int > {1, 1, 3},
new List< int > {2, 0, 0}
};
int Q = Queries.Count();
Solve(S, N, Queries, Q);
}}
|
Javascript
function solve(S, N, Queries, Q) {
let flip = 0;
for (let i = 0; i < Q; i++) {
let T = Queries[i][0],
A = Queries[i][1],
B = Queries[i][2];
A--;
B--;
if (T === 1) {
if (flip % 2 === 0) {
S = swap(S, A, B);
} else {
A = (A + N) % (2 * N);
B = (B + N) % (2 * N);
S = swap(S, A, B);
}
}
else {
flip++;
}
}
if (flip % 2 === 0) {
console.log(S);
} else {
let result = '' ;
for (let i = N; i < 2 * N; i++) {
result += S.charAt(i);
}
for (let i = 0; i < N; i++) {
result += S.charAt(i);
}
console.log(result);
}}
function swap(S, i, j) {
let arr = S.split( '' );
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr.join( '' );
}
const S = 'ABCD' ;
const N = Math.floor(S.length / 2);
const Queries = [[2, 0, 0], [1, 1, 3], [2, 0, 0]];
const Q = Queries.length;
solve(S, N, Queries, Q);
|
Time Complexity: O(N+Q)
Auxiliary Space: O(1)
Last Updated :
26 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...