Queries to flip characters of a binary string in given range
Given a binary string, str and a 2D array Q[][] representing queries of the form {L, R}. In each query, toggle all the characters of the binary strings present in the indices [L, R]. The task is to print the binary string by performing all the queries.
Examples:
Input: str = “101010”, Q[][] = { {0, 1}, {2, 5}, {2, 3}, {1, 4}, {0, 5} }
Output: 111000
Explanation:
Query 1: Toggling all the characters of str in the indices [0, 1]. Therefore, str becomes “011010”.
Query 2: Toggling all the characters of str in the indices [2, 5]. Therefore, str becomes “010101”.
Query 3: Toggling all the characters of str in the indices [2, 3]. Therefore, str becomes “011001”.
Query 4: Toggling all the characters of str in the indices [1, 4]. Therefore, str becomes “000111”.
Query 5: Toggling all the characters of str in the indices [0, 5]. Therefore, str becomes “111000”.
Therefore, the required binary string is “111000”.
Input: str = “00101”, Q[][]={ {0, 2}, {2, 3}, {1, 4} }
Output: 10000
Naive Approach: The simplest approach to solve the problem is to traverse all the queries arrays and toggle all the characters in the indices [L, R]. Finally, print the binary string.
Time Complexity: O(M * N), Where N denotes the length of the binary string.
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is to use the Prefix Sum array technique. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string toggleQuery(string str, int Q[][2],
int M)
{
int N = str.length();
int prefixCnt[N + 1] = { 0 };
for ( int i = 0; i < M; i++) {
prefixCnt[Q[i][0]] += 1;
prefixCnt[Q[i][1] + 1] -= 1;
}
for ( int i = 1; i <= N; i++) {
prefixCnt[i] += prefixCnt[i - 1];
}
for ( int i = 0; i < N; i++) {
if (prefixCnt[i] % 2) {
str[i] = '1' - str[i] + '0' ;
}
}
return str;
}
int main()
{
string str = "101010" ;
int Q[][2] = { { 0, 1 }, { 2, 5 },
{ 2, 3 }, { 1, 4 },
{ 0, 5 } };
int M = sizeof (Q) / sizeof (Q[0]);
cout << toggleQuery(str, Q, M);
}
|
Java
import java.util.*;
class GFG{
static String toggleQuery( char [] str,
int Q[][], int M)
{
int N = str.length;
int prefixCnt[] = new int [N + 1 ];
for ( int i = 0 ; i < M; i++)
{
prefixCnt[Q[i][ 0 ]] += 1 ;
prefixCnt[Q[i][ 1 ] + 1 ] -= 1 ;
}
for ( int i = 1 ; i <= N; i++)
{
prefixCnt[i] += prefixCnt[i - 1 ];
}
for ( int i = 0 ; i < N; i++)
{
if (prefixCnt[i] % 2 == 1 )
{
str[i] = ( char )( '1' -
str[i] + '0' );
}
}
return String.valueOf(str);
}
public static void main(String[] args)
{
String str = "101010" ;
int Q[][] = {{ 0 , 1 }, { 2 , 5 },
{ 2 , 3 }, { 1 , 4 },
{ 0 , 5 }};
int M = Q.length;
System.out.print(
toggleQuery(str.toCharArray(),
Q, M));
}
}
|
Python3
def toggleQuery(strr, Q, M):
strr = [i for i in strr]
N = len (strr)
prefixCnt = [ 0 ] * (N + 1 )
for i in range (M):
prefixCnt[Q[i][ 0 ]] + = 1
prefixCnt[Q[i][ 1 ] + 1 ] - = 1
for i in range ( 1 , N + 1 ):
prefixCnt[i] + = prefixCnt[i - 1 ]
for i in range (N):
if (prefixCnt[i] % 2 ):
strr[i] = ( chr ( ord ( '1' ) -
ord (strr[i]) +
ord ( '0' )))
return "".join(strr)
if __name__ = = '__main__' :
strr = "101010" ;
Q = [ [ 0 , 1 ],[ 2 , 5 ],
[ 2 , 3 ],[ 1 , 4 ],
[ 0 , 5 ] ]
M = len (Q)
print (toggleQuery(strr, Q, M))
|
C#
using System;
class GFG{
static String toggleQuery( char [] str,
int [,]Q ,
int M)
{
int N = str.Length;
int []prefixCnt = new int [N + 1];
for ( int i = 0; i < M; i++)
{
prefixCnt[Q[i, 0]] += 1;
prefixCnt[Q[i, 1] + 1] -= 1;
}
for ( int i = 1; i <= N; i++)
{
prefixCnt[i] += prefixCnt[i - 1];
}
for ( int i = 0; i < N; i++)
{
if (prefixCnt[i] % 2 == 1)
{
str[i] = ( char )( '1' -
str[i] + '0' );
}
}
return String.Join( "" , str);
}
public static void Main(String[] args)
{
String str = "101010" ;
int [,]Q = {{0, 1}, {2, 5},
{2, 3}, {1, 4},
{0, 5}};
int M = Q.GetLength(0);
Console.Write(toggleQuery(str.ToCharArray(),
Q, M));
}
}
|
Javascript
<script>
function toggleQuery(str, Q, M)
{
let N = str.length;
let prefixCnt = new Array(N + 1);
for (let i = 0; i < N + 1; i++)
{
prefixCnt[i] = 0;
}
for (let i = 0; i < M; i++)
{
prefixCnt[Q[i][0]] += 1;
prefixCnt[Q[i][1] + 1] -= 1;
}
for (let i = 1; i <= N; i++)
{
prefixCnt[i] += prefixCnt[i - 1];
}
for (let i = 0; i < N; i++)
{
if (prefixCnt[i] % 2 == 1)
{
str[i] = String.fromCharCode( '1' .charCodeAt(0) -
str[i].charCodeAt(0) + '0' .charCodeAt(0));
}
}
return (str).join( "" );
}
let str = "101010" ;
let Q = [[0, 1], [2, 5],
[2, 3], [1, 4],
[0, 5]];
let M = Q.length;
document.write(
toggleQuery(str.split( "" ),
Q, M));
</script>
|
Time Complexity: O(N + |Q|), Where N is the length of binary string.
Auxiliary Space: O(N)
Last Updated :
21 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...