A binary string S of length N is constructed from a string P of N characters and an integer X. The choice of the ith character of S is as follows:
- If the character Pi-X exists and is equal to 1, then Si is 1
- if the character Pi+X exists and is equal to 1, then Si is 1
- if both of the aforementioned conditions are false, then Si is 0.
Given the resulting string S and the integer X, reconstruct the original string P. If no string P can produce the string S, output -1.
Examples:
Input: S = “10011”, X = 2
Output: “01100”
Explanation: The input string S = “10011” can be constructed from the output string P = “01100”.
Input: S = “11101100111”, X = 3
Output: -1
Explanation: The input string S = “11101100111” cannot be constructed from any output string P.
Approach: The task can be solved by taking an auxiliary string with all 1s. Follow the below steps to solve the problem:
- Initialize the string P with all the characters as ‘1’.
- Now traverse the string S using a loop and put zeroes at the correct positions in P according to the given conditions:
- If S[i] is equal to ‘0’ and P[i-X] exists, i, e, (i-X)>=0, then put P[i-X] = ‘0’.
- If S[i] is equal to ‘0’ and P[i+X] exists, i.e, (i+X)<N, then put P[i+X] = ‘0’.
- Initialize a variable flag = 1 to determine whether the string P exists or not.
- To check for the correctness of the constructed string P, traverse the string S using a for loop:
- If S[i]==’1′ and either P[i-X] or P[i+X] exists and is equal to ‘1’, then the string P is so far correct and the traversal can continue.
- If S[i]==’1′ and either P[i-X] or P[i+X] does not exist or is not equal to 1, then set flag = 0, output -1 and break from the loop.
- If the flag is equal to 0 after the traversal of the string S, then output the original string P.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void findString(string S, int X)
{
int N = S.size();
string P(N, '1' );
for ( int i = 0; i < N; ++i) {
if (S[i] == '0' ) {
if (i - X >= 0) {
P[i - X] = '0' ;
}
if (i + X < N) {
P[i + X] = '0' ;
}
}
}
int flag = 1;
for ( int i = 0; i < N; ++i) {
if (S[i] == '1' ) {
if ((i - X >= 0
&& P[i - X] == '1' )
|| (i + X < N
&& P[i + X] == '1' )) {
continue ;
}
else {
flag = 0;
cout << -1;
break ;
}
}
}
if (flag == 1) {
cout << P;
}
}
int main()
{
string S = "10011" ;
int X = 2;
findString(S, X);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void findString(String S, int X)
{
int N = S.length();
char [] P = new char [N];
for ( int i = 0 ; i < N; ++i) {
P[i] = '1' ;
}
for ( int i = 0 ; i < N; ++i) {
if (S.charAt(i) == '0' ) {
if (i - X >= 0 ) {
P[i - X] = '0' ;
}
if (i + X < N) {
P[i + X] = '0' ;
}
}
}
int flag = 1 ;
for ( int i = 0 ; i < N; ++i) {
if (S.charAt(i) == '1' ) {
if ((i - X >= 0 && P[i - X] == '1' )
|| (i + X < N && P[i + X] == '1' )) {
continue ;
}
else {
flag = 0 ;
System.out.print(- 1 );
break ;
}
}
}
if (flag == 1 ) {
String p = new String(P);
System.out.print(p);
}
}
public static void main(String args[])
{
String S = "10011" ;
int X = 2 ;
findString(S, X);
}
}
|
Python3
def findString(S, X):
N = len (S)
P = [ '1' ] * N
for i in range (N):
if (S[i] = = '0' ):
if (i - X > = 0 ):
P[i - X] = '0' ;
if (i + X < N):
P[i + X] = '0' ;
flag = 1 ;
for i in range (N):
if (S[i] = = '1' ):
if ((i - X > = 0 and P[i - X] = = '1' ) or (i + X < N and P[i + X] = = '1' )):
continue ;
else :
flag = 0 ;
print ( - 1 );
break ;
if (flag = = 1 ):
print ("".join(P));
S = "10011" ;
X = 2 ;
findString(S, X);
|
C#
using System;
using System.Collections;
class GFG
{
static void findString( string S, int X)
{
int N = S.Length;
char [] P = new char [N];
for ( int i = 0; i < N; ++i) {
P[i] = '1' ;
}
for ( int i = 0; i < N; ++i) {
if (S[i] == '0' ) {
if (i - X >= 0) {
P[i - X] = '0' ;
}
if (i + X < N) {
P[i + X] = '0' ;
}
}
}
int flag = 1;
for ( int i = 0; i < N; ++i) {
if (S[i] == '1' ) {
if ((i - X >= 0 && P[i - X] == '1' )
|| (i + X < N && P[i + X] == '1' )) {
continue ;
}
else {
flag = 0;
Console.Write(-1);
break ;
}
}
}
if (flag == 1) {
string p = new string (P);
Console.Write(p);
}
}
public static void Main()
{
string S = "10011" ;
int X = 2;
findString(S, X);
}
}
|
Javascript
<script>
const findString = (S, X) => {
let N = S.length;
let P = new Array(N).fill( '1' );
for (let i = 0; i < N; ++i) {
if (S[i] == '0' ) {
if (i - X >= 0) {
P[i - X] = '0' ;
}
if (i + X < N) {
P[i + X] = '0' ;
}
}
}
let flag = 1;
for (let i = 0; i < N; ++i) {
if (S[i] == '1' ) {
if ((i - X >= 0
&& P[i - X] == '1' )
|| (i + X < N
&& P[i + X] == '1' )) {
continue ;
}
else {
flag = 0;
document.write(-1);
break ;
}
}
}
if (flag == 1) {
document.write(P.join( "" ));
}
}
let S = "10011" ;
let X = 2;
findString(S, X);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)