Decrypt the encoded string with help of Matrix as per given encryption decryption technique


In this problem, we need to decrypt the given ciphertext by traversing the matrix diagonally. We can solve the problem by traversing the matrix diagonally. Also, we require to traverse only the upper part of the matrix diagonally to get the decrypted string.

Problem statement – We have given an encrypted string of length N and row counts. We need to put the string in the matrix in a row-wise manner. After that, we need to traverse the matrix diagonally, starting from the [0, 0] index to decrypt the string.

Sample examples

Input

str = "TRSI_ _ _UIPN _ _ _TAOT_ _ _ OL", totalRows = 4

Output

TUTORIALSPOINT

Explanation – We can put the string in a row-wise manner, as shown below.

T

R

S

I

U

I

P

N

T

A

O

T

O

L

To decrypt the string, we can traverse it diagonally, as shown below.

  • T -> U -> T -> O -> R -> I -> A -> L -> S -> P -> O -> I -> N -> T

Input

str = "WCE_ _EO_ _ _LM"; totalRows = 3

Output

'WELCOME'

Explanation – The matrix for the encrypted string is given below.

W

C

E

E

O

L

M

To decrypt the string, we can traverse it diagonally, as shown below.

  • W -> E -> L -> C -> O -> M -> E

Approach 1

In this approach, we will use two nested loops to traverse the string as we use to traverse the 2D array. We will pick the character from the string so that it becomes the same as we pick the character from the matrix in a diagonal manner.

Algorithm

Step 1 – Initialize N with encrypted string size.

Step 2 – Get the total number of matrix columns by dividing N by the total rows.

Step 3 – Define the plaintext string to store the string after decryption.

Step 4 – Use the loop to make iterations from 0 to total columns.

Step 5 – Use the nested loop starting iteration from the pth index to N. Also, increase the value of q by totalColumns + 1 to go to the next row as we traverse diagonally.

Step 6 – Append the cipher[q] at the end of the plaintext.

Step 7 – Remove all spaces from the last of the plain text.

Step 8 – Return the plaintext value.

Example

#include <bits/stdc++.h>
using namespace std;

string getPlainText(string cipher, int totalRows){
    // Get string size
    int N = cipher.size();
    // Get total columns
    int totalColumns = ceil(N / (float)totalRows);
    // String to store plain text
    string plainText;
    // Traverse matrix to encode the string
    for (int p = 0; p < totalColumns; ++p){
        for (int q = p; q < N; q += totalColumns + 1){
            // Append character to string
            plainText += cipher[q];
        }
    }
    // Remove all spaces.
    while (plainText.back() == ' ') {
        plainText.pop_back();
    }
    return plainText;
}
int main(){
    string str = "TRSI   UIPN   TAOT   OL";
    int totalRows = 4;
    cout << "The decoded string is - " << getPlainText(str, totalRows) << endl;
    return 0;
}

Output

The decoded string is - TUTORIALSPOINT

Time complexity – O(N) as we traverse the string.

Space complexity – O(N) to store the decrypted string.

The problem's aim is that the programmer should learn to traverse the matrix diagonally. Here, we assume that a string is inserted in the matrix, and we traverse it diagonally, but programmers can use the same approach to traverse the real matrix diagonally.

Updated on: 24-Aug-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements