# Reconstruct original string from resultant string based on given encoding technique

In this problem, we need to construct the original string from the given string. The given string is formed from the original string using the given rules.

Here, we can use the given encryption rule and encrypted string to find the decrypted string by applying the encryption rule in reverse order.

Problem statement – We have given a binary string bin_str of length N and positive integer k. The binary string is constructed from the ‘enc’ string by following the below operations and using the x value.

• If enci-k is equal to 1, bin_stri is equal to 1.

• If enci+k is equal to 1, bin_stri is equal to 1.

• The bin_stri is 0 if above both conditions are false.

Sample examples

Input

bin_Str = "11001", k = 2


Output

00110


Explanation – Let’s understand how we can get each character of the bin_str string from the output string.

• bin_Str[0] = ‘1’ – In the output string, character at (0 + k) index is ‘1’.

• bin_Str[1] = ‘1’ – In the output string, character at (1 + k) index is ‘1’.

• bin_Str[2] = ‘0’ – In the output string, the character at (2 + k) and (2 – k) is 0.

• bin_Str[3] = ‘0’ - In the output string, the character at (3 + k) does not exist, and (3 – k) is 0.

• bin_Str[4] = ‘1’ - In the output string, the character at the (4 - k) index is ‘1’.

We choose the output string because we construct the bin_Str string from it according to the given encryption rules.

Input

bin_Str = "00011", k = 2


Output

-1


Explanation – The original string doesn’t exist.

## Approach 1

In this approach, we will create a string containing all 1’s. After that, we will modify the string according to the given encryption rules, and at last, we will cross-check whether we can get the original string.

### Algorithm

Step 1 – Initialize the ‘enc’ string with a total number of ‘1’s equal to the ‘bin_str’ string’s length.

Step 2 – Start traversing the string, and if the character at the pth index is ‘0’, it means either (p – k) or (p + k) character should be 0 if it exists. So, update enc[p-k] and enc[p+k] to ‘0’ if it exists.

Step 3 – In the ‘enc’ string, if the character at the pth index is ‘1’, follow the below steps.

Step 3.1 – If p – k exits, enc[p – k] equals 1, or p + k eixts and enc[p + k] equals 1, continue to the next iteration.

Step 3.2 – Otherwise, return ‘-1’ as the string doesn’t exist.

Step 4 – Return the ‘enc’ string at last.

### Example

#include <iostream>
using namespace std;

string generateString(string bin_Str, int K) {
// Get string size
int len = bin_Str.size();
// Initialize the string with all 1's
string enc(len, '1');
// Traverse string to update
for (int p = 0; p < len; ++p) {
// For the character '0'
if (bin_Str[p] == '0') {
// If p-k or p + k exists, set it to '0' according to the reverse of the encryption condition
if (p - K >= 0) {
enc[p - K] = '0';
}
if (p + K < len) {
enc[p + K] = '0';
}
}
}
// Cross check resultant string
for (int p = 0; p < len; ++p) {
// For character '1'
if (bin_Str[p] == '1') {
// If p - k and p + k is valid and is equal to '1', continue.
if ((p - K >= 0 && enc[p - K] == '1') || (p + K < len && enc[p + K] == '1')) {
continue;
} else {
return "-1";
break;
}
}
}
return enc;
}
int main() {
// Given input
string bin_Str = "11001";
int K = 2;
string result = generateString(bin_Str, K);
if (result == "-1") {
cout << "The required string is not exist!";
} else {
cout << "The original string is: " << result << endl;
}

return 0;
}


### Output

The original string is: 00110


Time complexity – O(N) to iterate the string.

Space complexity – O(N) for the auxiliary string.

In the problem solution, we used the encryption rules to get the original string from the encrypted string. We first changed 1’s to 0 by following the first condition. After that, we ensure that the string follows the first and second conditions. So, if we follow the encryption rules in reverse order, we can decrypt the string.

Updated on: 25-Aug-2023

29 Views