Ninja And His Secret Information

Posted: 13 Mar, 2021
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

Ninja has recently joined the FBI. Ninja got some secret information ‘SECRET_INFORMATION’ which he wants to share with his team. But he can not send this information directly because of the hackers and security threats they pose.

So Ninja decides to encode this ‘SECRET_INFORMATION’ and then send the encoded ‘SECRET_INFORMATION’ to his team members. This way, only his team members can decode this information and thus share it safely.

As Ninja is busy extracting the ‘SECRET_INFORMATION, he asks you for help.

Can you help Ninja and his friends with encoding and decoding the ‘SECRET_INFORMATION’?

Note:

1. The ‘SECRET_INFORMATION’ may contain any possible character out of 256 valid ASCII characters.
2. While decoding the ‘SECRET_INFORMATION’ do not use class members / global/static variables.
3. Do not use any inbuilt library method/function for decoding the ‘SECRET_INFORMATION’.
Input Format:
The first line of input contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases follow.

The first and the only line of each test case contains an input string ‘STR’ 
Output Format :
For each test case, design an algorithm that returns the encoded and decoded string for each input ‘SECRET_INFORMATION’. The output is “Transmission successful” if decoding the encoded string gives the same string as ‘SECRET_INFORMATION’ else the output is “Transmission failed”. 

The output of each test case will be printed in a separate line.

Note:

You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= ‘T’ <= 100
1 <= |SECRET_INFORMATION| <= 5000

Where ‘T’ denotes the total number of test cases and |SECRET_INFORMATION| represents the length of ‘SECRET_INFORMATION’ Ninja has to transfer to his team.

Time Limit: 1 second
Approach 1

For encoding the given ‘SECRET_INFORMATION’ we can use any algorithm. Here first, we append the number of words of ‘SECRET_INFORMATION’ and ‘!’ into our resultant decoding string ‘ENCODED_INFORMATION’. Then we append the length of each word and ‘!’. At last, we find the ‘ASCII’ value of each character and then add 3 into the ASCII value. Then we convert this ASCII value into a character and replace this with the actual character of ‘SECRET_INFORMATION’ and append ‘!’ after each word of the ‘SECRET_INFORMATION’ into the ‘ENCODED_INFORMATION’.

 

For decoding the ‘ENCODED_INFORMATION’  we follow the exact same steps in the same order which we followed for finding the ‘ENCODED_INFORMATION’. Only one difference is for extracting the actual character of the ‘SECRET_INFORMATION’. First, we find the ASCII value of each character and subtract 3 into the ASCII value. Then we convert this ASCII value into character.

  

Here is the algorithm:

For Encoding:

  1. We declare a variable ‘ENCODED_INFORMATION’ in which we store encoded information.
  2. Append the number of words of ‘SECRET_INFORMATION’ to the ‘ENCODED_INFORMATION’.
  3. We ran a loop for ‘i’ = 0 to the number of words of ‘SECRET_INFORMATION’.
    1. Append ‘!’ and length of the current word of ‘SECRET_INFORMATION’.
  4. Append ‘!’ to the ‘ENCODED_INFORMATION’.
  5. We run a loop for ‘i’ = 0 to the number of words of ‘SECRET_INFORMATION’.
    1. Append current word of ‘SECRET_INFORMATION’ into ‘ENCODED_INFORMATION’.
  6. Finally, return ‘ENCODED_INFORMATION’.

For Decoding:

  1. We declare a variable ‘DECODED_INFORMATION’ in which we store decoded information.
  2. We declare a variable ‘NUM_OF_WORDS’ in which we store the number of words present in ‘SECRET_INFORMATION’.
  3. We run a loop while char of ‘DECODED_INFORMATION’ is not equal to ‘!’:
    1. Convert the character of ‘ENCODED_INFORMATION’ into an integer and append it into ‘NUM_OF_WORDS’.
  4. We declare a  variable ‘LENGTH_OF_EACH_WORDS’ in which we store the length of each word of the ‘SECRET_INFORMATION’.
  5. We run a loop while ‘NUM_OF_WORDS’:
    1. Store the length of each word into ‘LENGTH_OF_EACH_WORDS’.
  6. We run a loop for ‘i’ = o to the length of ‘LENGTH_OF_EACH_WORDS’:
    1. Append each word into the ‘DECODED_INFORMATION’ followed by space.
  7. Finally, return the ‘DECODED_INFORMATION’.
Try Problem