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++ program for the above approach
#include <iostream>
using namespace std;
// Function to find the original string P
void findString(string S, int X)
{
// Stores the size of string S
int N = S.size();
// Each character of string
// P is set to '1'
string P(N, '1');
// Loop to put zeroes at
// the correct positions in P
for (int i = 0; i < N; ++i) {
// If the character is '0'
if (S[i] == '0') {
// If P[i-X] exists
if (i - X >= 0) {
// Set P[i-X] to '0'
P[i - X] = '0';
}
// If P[i+X] exists
if (i + X < N) {
// Set P[i+X] to '0'
P[i + X] = '0';
}
}
}
// Set flag to 1
int flag = 1;
// Loop to cross check
// if P exists or not
for (int i = 0; i < N; ++i) {
// If the character is '1'
if (S[i] == '1') {
// If P[i-X] exists and
// is equal to '1' or if
// P[i+X] exists and
// is equal to '1'
if ((i - X >= 0
&& P[i - X] == '1')
|| (i + X < N
&& P[i + X] == '1')) {
continue;
}
else {
// Set flag to 0
flag = 0;
// Output -1
cout << -1;
// Break from the loop
break;
}
}
}
// If flag is equal to 1
if (flag == 1) {
// Output string P
cout << P;
}
}
// Driver Code
int main()
{
// Given input
string S = "10011";
int X = 2;
// Function Call
findString(S, X);
return 0;
}
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the original string P
static void findString(String S, int X)
{
// Stores the size of string S
int N = S.length();
// Each character of string
// converted to char Array P
// is set to '1'
char[] P = new char[N];
for (int i = 0; i < N; ++i) {
P[i] = '1';
}
// Loop to put zeroes at
// the correct positions in P
for (int i = 0; i < N; ++i) {
// If the character is '0'
if (S.charAt(i) == '0') {
// If P[i-X] exists
if (i - X >= 0) {
// Set P[i-X] to '0'
P[i - X] = '0';
}
// If P[i+X] exists
if (i + X < N) {
// Set P[i+X] to '0'
P[i + X] = '0';
}
}
}
// Set flag to 1
int flag = 1;
// Loop to cross check
// if P exists or not
for (int i = 0; i < N; ++i) {
// If the character is '1'
if (S.charAt(i) == '1') {
// If P[i-X] exists and
// is equal to '1' or if
// P[i+X] exists and
// is equal to '1'
if ((i - X >= 0 && P[i - X] == '1')
|| (i + X < N && P[i + X] == '1')) {
continue;
}
else {
// Set flag to 0
flag = 0;
// Output -1
System.out.print(-1);
// Break from the loop
break;
}
}
}
// If flag is equal to 1
if (flag == 1) {
// Output string P
String p = new String(P);
System.out.print(p);
}
}
// Driver Code
public static void main(String args[])
{
// Given input
String S = "10011";
int X = 2;
// Function Call
findString(S, X);
}
}
// This code is contributed by Samim Hossain Mondal.
# Python program for the above approach
# Function to find the original string P
def findString(S, X):
# Stores the size of string S
N = len(S)
# Each character of string
# P is set to '1'
P = ['1'] * N
# Loop to put zeroes at
# the correct positions in P
for i in range(N):
# If the character is '0'
if (S[i] == '0'):
# If P[i-X] exists
if (i - X >= 0):
# Set P[i-X] to '0'
P[i - X] = '0';
# If P[i+X] exists
if (i + X < N):
# Set P[i+X] to '0'
P[i + X] = '0';
# Set flag to 1
flag = 1;
# Loop to cross check
# if P exists or not
for i in range(N):
# If the character is '1'
if (S[i] == '1'):
# If P[i-X] exists and
# is equal to '1' or if
# P[i+X] exists and
# is equal to '1'
if ((i - X >= 0 and P[i - X] == '1') or (i + X < N and P[i + X] == '1')):
continue;
else:
# Set flag to 0
flag = 0;
# Output -1
print(-1);
# Break from the loop
break;
# If flag is equal to 1
if (flag == 1):
# Output string P
print("".join(P));
# Driver Code
# Given input
S = "10011";
X = 2;
# Function Call
findString(S, X);
# This code is contributed by gfgking
// C# program for the above approach
using System;
using System.Collections;
class GFG
{
// Function to find the original string P
static void findString(string S, int X)
{
// Stores the size of string S
int N = S.Length;
// Each character of string
// converted to char Array P
// is set to '1'
char[] P = new char[N];
for (int i = 0; i < N; ++i) {
P[i] = '1';
}
// Loop to put zeroes at
// the correct positions in P
for (int i = 0; i < N; ++i) {
// If the character is '0'
if (S[i] == '0') {
// If P[i-X] exists
if (i - X >= 0) {
// Set P[i-X] to '0'
P[i - X] = '0';
}
// If P[i+X] exists
if (i + X < N) {
// Set P[i+X] to '0'
P[i + X] = '0';
}
}
}
// Set flag to 1
int flag = 1;
// Loop to cross check
// if P exists or not
for (int i = 0; i < N; ++i) {
// If the character is '1'
if (S[i] == '1') {
// If P[i-X] exists and
// is equal to '1' or if
// P[i+X] exists and
// is equal to '1'
if ((i - X >= 0 && P[i - X] == '1')
|| (i + X < N && P[i + X] == '1')) {
continue;
}
else {
// Set flag to 0
flag = 0;
// Output -1
Console.Write(-1);
// Break from the loop
break;
}
}
}
// If flag is equal to 1
if (flag == 1) {
// Output string P
string p = new string(P);
Console.Write(p);
}
}
// Driver Code
public static void Main()
{
// Given input
string S = "10011";
int X = 2;
// Function Call
findString(S, X);
}
}
// This code is contributed by Taranpreet
<script>
// JavaScript program for the above approach
// Function to find the original string P
const findString = (S, X) => {
// Stores the size of string S
let N = S.length;
// Each character of string
// P is set to '1'
let P = new Array(N).fill('1');
// Loop to put zeroes at
// the correct positions in P
for (let i = 0; i < N; ++i) {
// If the character is '0'
if (S[i] == '0') {
// If P[i-X] exists
if (i - X >= 0) {
// Set P[i-X] to '0'
P[i - X] = '0';
}
// If P[i+X] exists
if (i + X < N) {
// Set P[i+X] to '0'
P[i + X] = '0';
}
}
}
// Set flag to 1
let flag = 1;
// Loop to cross check
// if P exists or not
for (let i = 0; i < N; ++i) {
// If the character is '1'
if (S[i] == '1') {
// If P[i-X] exists and
// is equal to '1' or if
// P[i+X] exists and
// is equal to '1'
if ((i - X >= 0
&& P[i - X] == '1')
|| (i + X < N
&& P[i + X] == '1')) {
continue;
}
else {
// Set flag to 0
flag = 0;
// Output -1
document.write(-1);
// Break from the loop
break;
}
}
}
// If flag is equal to 1
if (flag == 1) {
// Output string P
document.write(P.join(""));
}
}
// Driver Code
// Given input
let S = "10011";
let X = 2;
// Function Call
findString(S, X);
// This code is contributed by rakeshsahni
</script>
Output:
01100
Time Complexity: O(N)
Auxiliary Space: O(N)