# Encoded String DECODEIT SOLUTION Code Chef

Page Contents

## Encoded String DECODEIT SOLUTION Code Chef

An encoder encodes the first 1616 lowercase English letters using 44 bits each. The first bit (from the left) of the code is 00 if the letter lies among the first 88 letters, else it is 11, signifying that it lies among the last 88 letters. The second bit of the code is 00 if the letter lies among the first 44 letters of those 88 letters found in the previous step, else it’s 11, signifying that it lies among the last 44 letters of those 88 letters. Similarly, the third and the fourth bit each signify the half in which the letter lies.

For example, the letter jj would be encoded as :

• Among (a,b,c,d,e,f,g,h(a,b,c,d,e,f,g,h || i,j,k,l,m,n,o,p)i,j,k,l,m,n,o,p), jj appears in the second half. So the first bit of its encoding is 11.
• Now, among (i,j,k,l(i,j,k,l || m,n,o,p)m,n,o,p), jj appears in the first half. So the second bit of its encoding is 00.
• Now, among (i,j(i,j || k,l)k,l), jj appears in the first half. So the third bit of its encoding is 00.
• Now, among (i(i || j)j), jj appears in the second half. So the fourth and last bit of its encoding is 11.

So jj’s encoding is 10011001,

Given a binary encoded string SS, of length at most 105105, decode the string. That is, the first 4 bits are the encoding of the first letter of the secret message, the next 4 bits encode the second letter, and so on. It is guaranteed that the string’s length is a multiple of 4.

### Input:

• The first line of the input contains an integer TT, denoting the number of test cases.
• The first line of each test case contains an integer NN, the length of the encoded string.
• The second line of each test case contains the encoded string SS.

### Output:

For each test case, print the decoded string, in a separate line.

### Constraints

• 1≤T≤101≤T≤10
• 4≤N≤1054≤N≤105
• The length of the encoded string is a multiple of 44.
• 0≤Si≤10≤Si≤1

• 100100 points : Original constraints.

```3
4
0000
8
00001111
4
1001
```

```a
ap
j
```

### Explanation:

• Sample Case 11 :

The first bit is 00, so the letter lies among the first 88 letters, i.e., among a,b,c,d,e,f,g,ha,b,c,d,e,f,g,h. The second bit is 00, so it lies among the first four of these, i.e., among a,b,c,da,b,c,d.

The third bit is 00, so it again lies in the first half, i.e., it’s either aa or bb. Finally, the fourth bit is also 00, so we know that the letter is aa.

• Sample Case 22 :

Each four bits correspond to a character. Just like in sample case 11, 00000000 is equivalent to aa. Similarly, 11111111 is equivalent to pp. So, the decoded string is apap.

• Sample Case 33 :

The first bit is 11, so the letter lies among the last 88 letters, i.e., among i,j,k,l,m,n,o,pi,j,k,l,m,n,o,p. The second bit is 00, so it lies among the first four of these, i.e., among i,j,k,li,j,k,l.

The third bit is 00, so it again lies in the first half, i.e., it’s either ii or jj. Finally, the fourth bit is 11, so we know that the letter is jj.

## Solution

Program Python:

```for test in range(int(input())):
n = int(input())
bin_str = input()
for i in range(0, n, 4):
x = ""
for j in range(4):
x = x+bin_str[i+j]
ans = chr(int(x, 2)+97)
print(ans, end="")
print()```

Program C++:

```#include <iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
int test;
cin>>test;
while(test--)
{
string strs, r="";

long long int l,ctr=0,p=1,b=0;
cin>>l;
cin>>strs;
for(int i=l-1;i>=0;i--)
{
b=b+p*(strs[i]-'0');
p=p*2;
ctr++;
if(ctr == 4)
{
char c=(char)(97+b);
r.push_back(c);
ctr=0;
p=1;
b=0;
}
}
reverse(r.begin(),r.end());
cout<<r<<endl;
r.clear();
strs.clear();

}

return 0;
}```

Program Java:

```import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
try{
while (t-- != 0) {

String s;
String ans = "";
for (int i = 0; i < size; i = i + 4) {
if ((i + 4) == size)
s = str.substring(i);
else
s = str.substring(i, i + 4);

int lower = 0;
int mid;

String arr = "abcdefghijklmnop";
int higher = arr.length();
String a = "";
for (int j = 0; j < s.length(); j++) {

if (s.charAt(j) == '0') {
mid = (lower + higher) / 2;
a = arr.substring(lower, mid);
higher = mid;
} else {
mid = (lower + higher) / 2;
if (higher == 16)
a = arr.substring(mid);
else
a = arr.substring(mid, higher);
lower = mid;
}
}
ans += a;
}
System.out.println(ans);

}
}catch(Exception e){
return;
}
}
}```