Decrypt the encoded string with help of Matrix as per given encryption decryption technique
Last Updated :
14 Mar, 2022
Given an encoded (or encrypted) string S of length N, an integer M. The task is to decrypt the encrypted string and print it. The encryption and decryption techniques are given as:
Encryption: The original string is placed in a Matrix of M rows and N/M columns, such that the first character of the Original text or string is placed on the top-left cell to the bottom-right manner. If last row is reached, then again go to the top row, and start from the next column.
For example: If string is "geeks", M = 2, then the string will be encrypted in below manner
Then traverse the matrix row wise and print the characters as they appear.
Therefore the above string will be encrypted as = "ges ek"
Decryption: Traverse the matrix diagonally in the same manner as it was encrypted and find the actual string.

Examples:
Input: "GSRE_ _ _E_ _K_ _ _EFGS_ _ _KOE" (Here '_' means a space), 4
Output: "GEEKS FOR GEEKS"
Explanation: See the image below for understanding the approach.
Here column number is 6.
So, the traversing starts from (0, 0) position, then goes upto the end of the row, means from (0, 0) --> (1, 1)-->(2, 2)-->(3, 3)-->(4, 4). T
hen get to the first row, and start from the next, means from (0, 1)-->(1, 2)-->(2, 3)->(3, 4) and continues upto it reaches to the end of the given string.
Input: "GEEKSFORGEEKS", 1
Output: "GEEKSFORGEEKS"
Explanation: There is only one row. so the decoded string will be as same as the given encoded string.
Input: "abc de", 2
Output: "adbec"
Approach: First, find the number of columns. Then, start traversing. Follow the below steps to solve the problem:
- For each of the columns, go up to the length of the string starting from the i'th row, and after each traversal increase the iterating value to column+1.
- Because, here traversal is done diagonally, and here the next diagonal character will be after column number plus one.
For example, in the below picture, the X is a character, whose next diagonal character is XX, and the column number where X is present is 2, the next character row number is just one greater than the previous.

- At the time of traversing add the characters into an empty string. Then check, if there is any space at the end of the string or not, if it is, then just delete it.
At last, print that string, and this will be the decoded string/desired string.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the desire string
string decodeString(string encodedText, int rows)
{
// Stores the length of the string
int len = encodedText.size();
// Stores the number of columns
int cols = len / rows;
// Declaring an empty string
string res;
for (int i = 0; i < cols; ++i)
for (int j = i; j < len; j += cols + 1)
// Adding the characters
// into the empty string
res += encodedText[j];
// If their any space at the end,
// delete it
while (res.back() == ' ') {
res.pop_back();
}
return res;
}
// Driver Code
int main()
{
string S = "GSRE E K EFGS KOE";
int row = 4;
cout << decodeString(S, row) << endl;
return 0;
}
Java
// Java program for the above approach
class GFG {
// Function to find the desire string
static String decodeString(String encodedText, int rows) {
// Stores the length of the string
int len = encodedText.length();
// Stores the number of columns
int cols = len / rows;
// Declaring an empty string
String res = "";
for (int i = 0; i < cols; ++i)
{
for (int j = i; j < len; j += cols + 1)
{
// Adding the characters
// into the empty string
res += encodedText.charAt(j);
}
}
// If their any space at the end,
// delete it
while (res.charAt(res.length() - 1) == ' ') {
res = res.substring(0, res.length() - 2);
}
return res;
}
// Driver Code
public static void main(String args[]) {
String S = "GSRE E K EFGS KOE";
int row = 4;
System.out.println(decodeString(S, row));
}
}
// This code is contributed by gfgking
Python3
# Python code for the above approach
# Function to find the desire string
def decodeString(encodedText, rows):
# Stores the length of the string
_len = len(encodedText)
# Stores the number of columns
cols = _len // rows
# Declaring an empty string
res = "";
for i in range(cols):
for j in range(i, _len, cols + 1):
# Adding the characters
# into the empty string
res += encodedText[j];
# If their any space at the end,
# delete it
while (res[len(res) - 1] == ' '):
res = res[0: len(res) - 1];
return res;
# Driver Code
S = "GSRE E K EFGS KOE";
row = 4;
print(decodeString(S, row))
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the desire string
static string decodeString(string encodedText, int rows)
{
// Stores the length of the string
int len = encodedText.Length;
// Stores the number of columns
int cols = len / rows;
// Declaring an empty string
string res = "";
for (int i = 0; i < cols; ++i) {
for (int j = i; j < len; j += cols + 1) {
// Adding the characters
// into the empty string
res += encodedText[j];
}
}
// If their any space at the end,
// delete it
while (res[res.Length - 1] == ' ') {
res = res.Remove(res.Length - 1);
}
return res;
}
// Driver Code
public static void Main()
{
string S = "GSRE E K EFGS KOE";
int row = 4;
Console.Write(decodeString(S, row));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the above approach
// Function to find the desire string
function decodeString(encodedText, rows) {
// Stores the length of the string
let len = encodedText.length;
// Stores the number of columns
let cols = Math.floor(len / rows);
// Declaring an empty string
let res = "";
for (let i = 0; i < cols; ++i)
for (let j = i; j < len; j += cols + 1)
// Adding the characters
// into the empty string
res += encodedText[j];
// If their any space at the end,
// delete it
while (res[res.length - 1] == ' ') {
res = res.slice(0, res.length - 1);
}
return res;
}
// Driver Code
let S = "GSRE E K EFGS KOE";
let row = 4;
document.write(decodeString(S, row))
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(M*(M/col)) which is near about O(length of the string)
Auxiliary Space: O(N)
Similar Reads
Decrypt message from given code by replacing all * with prefix values of encoded string Given a string str of length of N that is in the encoded form with alphabets and * . The task is to find the string from which it was generated. The required string can be generated from the encoded string by replacing all the * with the prefix values of the encoded string. Examples: Input: str = ab
4 min read
Encrypt the given string with the following operations Given a string s, the task is to encrypt the string in the following way: If the frequency of current character is even, then increment current character by x.If the frequency of current character is odd, then decrement current character by x. Note: All the operations are circular that is adding 1 t
6 min read
Find the string among given strings represented using given encryption pattern Given an array of strings arr[] of size N and an encrypted string str, the task is to find the correct string from the given array of strings whose encryption will give str where str is encrypted using the following rules: The starting characters form an integer representing the number of uppercase
8 min read
Decrypt a string encrypted by repeating i-th character i times Given an encrypted string str and the encryption algorithm, the task is to decrypt the string. The encryption algorithm is as follows: The 1st character of the string will be repeated once in the encrypted string, the 2nd character will be repeated twice, ..., nth character will be repeated n times.
4 min read
Encode given String by inserting in Matrix column-wise and printing it row-wise Given a string S and an integer R, the task is to encode the string by first filling each character in column wise manner from top to bottom in a matrix having R rows and then printing the matrix row-wise. Examples: Input: S = "abcdefgh", R = 3Output: adgbehcfExplanation: Matrix formed is:a d gb e h
4 min read
Encode given string by replacing substrings with prefix same as itself with * Given string str of size N containing only lowercase English letters. The task is to encrypt the string such that the substrings having same prefix as itself are replaced by a *. Generate the encrypted string. Note: If the string can be encrypted in multiple ways, find the smallest encrypted string.
9 min read
Restore original String from given Encrypted String by the given operations Given a string str and a positive integer N, the task is to reverse N characters and skip N characters until the end of the string to generate the encrypted message. Examples: Input: str = "ihTs suohld ebeas!y", K = 3 Output: This should be easy! Explanation: Reverse "ihT" -> "Thi" "s" remains th
5 min read
Encrypt a string by repeating i-th character i times Given string str, the task is to encrypt the string with the given encryption algorithm. The 1st character of the string will be repeated once in the encrypted string, the 2nd character will be repeated twice, â¦, nth character will be repeated n times. Examples: Input: str = "geeks" Output: geeeeekk
4 min read
Count possible decoding of a given digit sequence with hidden characters Given a string S containing digits and character '*' i.e. hidden character, the task is to find the number of ways to decode this hidden character of the given string. Since the answer may be very large, return it modulo 109+7. A string containing letters from A-Z can be encoded into numbers using t
15+ min read
Check if the rows of a binary matrix can be made unique by removing a single column Given a binary matrix mat[][] of size M * N. The task is to check whether the row of the matrix will be unique after deleting a column from the matrix. Example: Input: mat[][] = { {1 0 1}, {0 0 0}, {1 0 0} } Output: Yes After deleting 2nd column each row of matrix become unique. Input:mat[][] = { {1
6 min read