Decrypt the String according to given algorithm
Last Updated :
18 Aug, 2021
Given encrypted string str consisting of alphabets and numeric characters, the task is to decrypt the string and find the encrypted message.
In order to decrypt the message, find every cluster of numeric characters representing a single alphabetic character which can be obtained by calculating the modulus of the number with 26 and the found value from the range [0, 25] can be mapped with the characters ['a', 'z'].
For example, if str = "32ytAAcV4ui30hf10hj18" then the clusters of numeric characters will be {32, 4, 30, 10, 18} which gives {6, 4, 4, 10, 18} after performing modulus with 26 and can be mapped to {'g', 'e', 'e', 'k', 's'}
Examples:
Input: str = "this50hi4huji70"
Output: geeks
Input: str = "a1s2d3f3"
Output: bcdd
Approach: The idea is to traverse each character one by one in a string, then check if it is a numeric character or not. If it is then concatenate it into a string. Finally, modulo that number string with 26.
While doing modulo operation, we can't simply do x % 26 as the number can be too large and will result in an integer overflow.
To handle this, we will process all digits one by one and use the property that
(A * B) mod C = ((A mod C) * (B mod C)) mod C
Then, finally decode back each integer to character by 0, 1, 2, 3, ...25 to 'a', 'b', 'c', ...'z', concatenate all characters and print the final result.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
const int MOD = 26;
// Function that returns (num % 26)
int modulo_by_26(string num)
{
// Initialize result
int res = 0;
// One by one process all digits of 'num'
for (int i = 0; i < num.length(); i++)
res = (res * 10 + (int)num[i] - '0') % MOD;
return res;
}
// Function to return the decrypted string
string decrypt_message(string s)
{
// To store the final decrypted answer
string decrypted_str = "";
string num_found_so_far = "";
// One by one check for each character
// if it is a numeric character
for (int i = 0; i < s.length(); ++i) {
if (s[i] >= '0' && s[i] <= '9') {
num_found_so_far += s[i];
}
else if (num_found_so_far.length() > 0) {
// Modulo the number found in the string by 26
decrypted_str += 'a'
+ modulo_by_26(num_found_so_far);
num_found_so_far = "";
}
}
if (num_found_so_far.length() > 0) {
decrypted_str += 'a'
+ modulo_by_26(num_found_so_far);
}
return decrypted_str;
}
// Driver code
int main()
{
string s = "32ytAAcV4ui30hf10hj18";
// Print the decrypted string
cout << decrypt_message(s);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG{
static int MOD = 26;
// Function that returns (num % 26)
static int modulo_by_26(String num)
{
// Initialize result
int res = 0;
// One by one process all digits of 'num'
for(int i = 0; i < num.length(); i++)
{
res = ((res * 10 +
(int)num.charAt(i) - '0') % MOD);
}
return res;
}
// Function to return the decrypted String
static String decrypt_message(String s)
{
// To store the final decrypted answer
String decrypted_str = "";
String num_found_so_far = "";
// One by one check for each character
// if it is a numeric character
for(int i = 0; i < s.length(); ++i)
{
if (s.charAt(i) >= '0' && s.charAt(i) <= '9')
{
num_found_so_far += s.charAt(i);
}
else if (num_found_so_far.length() > 0)
{
// Modulo the number found in the String by 26
decrypted_str += (char)('a' +
modulo_by_26(num_found_so_far));
num_found_so_far = "";
}
}
if (num_found_so_far.length() > 0)
{
decrypted_str += (char)('a' +
modulo_by_26(num_found_so_far));
}
return decrypted_str;
}
// Driver code
public static void main(String[] args)
{
String s = "32ytAAcV4ui30hf10hj18";
// Print the decrypted string
System.out.print(decrypt_message(s));
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 implementation of
# the approach
MOD = 26
# Function that returns
# (num % 26)
def modulo_by_26(num):
# Initialize result
res = 0
# One by one process all
# digits of 'num'
for i in range(len(num)):
res = ((res * 10 + ord(num[i]) -
ord('0')) % MOD)
return res
# Function to return the
# decrypted string
def decrypt_message(s):
# To store the final
# decrypted answer
decrypted_str = ""
num_found_so_far = ""
# One by one check for
# each character if it
# is a numeric character
for i in range(len(s)):
if (s[i] >= '0' and
s[i] <= '9'):
num_found_so_far += s[i]
elif (len(num_found_so_far) > 0):
# Modulo the number found
# in the string by 26
decrypted_str += chr(ord('a') +
(modulo_by_26(num_found_so_far)))
num_found_so_far = ""
if (len(num_found_so_far) > 0):
decrypted_str += chr(ord('a') +
(modulo_by_26(num_found_so_far)))
return decrypted_str
# Driver code
if __name__ == "__main__":
s = "32ytAAcV4ui30hf10hj18"
# Print the decrypted string
print(decrypt_message(s))
# This code is contributed by Chitranayal
C#
// C# implementation of the approach
using System;
class GFG{
static int MOD = 26;
// Function that returns (num % 26)
static int modulo_by_26(String num)
{
// Initialize result
int res = 0;
// One by one process all digits of 'num'
for(int i = 0; i < num.Length; i++)
{
res = ((res * 10 +
(int)num[i] - '0') % MOD);
}
return res;
}
// Function to return the decrypted String
static String decrypt_message(String s)
{
// To store the readonly decrypted answer
String decrypted_str = "";
String num_found_so_far = "";
// One by one check for each character
// if it is a numeric character
for(int i = 0; i < s.Length; ++i)
{
if (s[i] >= '0' && s[i] <= '9')
{
num_found_so_far += s[i];
}
else if (num_found_so_far.Length > 0)
{
// Modulo the number found
// in the String by 26
decrypted_str += (char)('a' +
modulo_by_26(num_found_so_far));
num_found_so_far = "";
}
}
if (num_found_so_far.Length > 0)
{
decrypted_str += (char)('a' +
modulo_by_26(num_found_so_far));
}
return decrypted_str;
}
// Driver code
public static void Main(String[] args)
{
String s = "32ytAAcV4ui30hf10hj18";
// Print the decrypted string
Console.Write(decrypt_message(s));
}
}
// This code is contributed by amal kumar choubey
JavaScript
<script>
// JavaScript implementation of the approach
let MOD = 26;
// Function that returns (num % 26)
function modulo_by_26(num)
{
// Initialize result
let res = 0;
// One by one process all digits of 'num'
for(let i = 0; i < num.length; i++)
{
res = ((res * 10 +
num[i].charCodeAt(0) - '0'.charCodeAt(0)) % MOD);
}
return res;
}
// Function to return the decrypted String
function decrypt_message(s)
{
// To store the final decrypted answer
let decrypted_str = "";
let num_found_so_far = "";
// One by one check for each character
// if it is a numeric character
for(let i = 0; i < s.length; ++i)
{
if (s[i].charCodeAt(0) >= '0'.charCodeAt(0) &&
s[i].charCodeAt(0) <= '9'.charCodeAt(0))
{
num_found_so_far += s[i];
}
else if (num_found_so_far.length > 0)
{
// Modulo the number found in the String by 26
decrypted_str += String.fromCharCode('a'.charCodeAt(0) +
modulo_by_26(num_found_so_far));
num_found_so_far = "";
}
}
if (num_found_so_far.length > 0)
{
decrypted_str += String.fromCharCode('a'.charCodeAt(0) +
modulo_by_26(num_found_so_far));
}
return decrypted_str;
}
// Driver code
let s = "32ytAAcV4ui30hf10hj18";
// Print the decrypted string
document.write(decrypt_message(s));
// This code is contributed by rag2127
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Decode the string encoded with the given algorithm Given a decoded string str which was decoded with the following encoding algorithm: Write down the middle character of the string then delete it and repeat the process until there are no characters left. For example, "abba" will be encoded as "bbaa". Note that the middle character is the first chara
7 min read
Construct the Cypher string based on the given conditions Given a number N, the task is to convert the given number into a Cypher string on the basis of below conditions: If N is a semiprime, then change every digit at even places of N to it's corresponding matched alphabet as shown below.If N can be written as a sum of two primes, then change every digit
12 min read
Program to find the kth character after decrypting a string Given a string str consisting of characters and numbers and an integer k, the task is to decrypt the string and the return the kth character in the decrypted string.In order to decrypt the string, traverse the string character by character and if the current character is an alphabet then append it t
8 min read
Caesar Concatenation Given two strings str1 and str2 containing alpha-numeric characters and a number N. The task is to form a new encrypted string which contains the string str1 with a Caesar Encryption of N characters and the string str2 with a Caesar Encryption of N characters at odd indices.Example: Input: str1 = "G
9 min read
Decrypt a string according to given rules Given encrypted string str, the task is to decrypt the given string when the encryption rules are as follows: Start with the first character of the original string.In every odd step, append the next character to it.In every even step, prepend the next character to the encrypted string so far.For exa
15+ min read
Move all digits to the beginning of a given string Given a string S, the task is to move all the digits present in the string, to the beginning of the string. Examples: Input: S = âGeeks4forGeeks123âOutput: 4123GeeksforGeeksExplanation:The given string contains digits 4, 1, 2, and 3. Moving all the digits to the beginning of the string modifies the
7 min read
Add index to characters and reverse the string Given a string str, the task is to encrypt and reverse the string. The string is encrypted by adding every character of the string with it's index in the string i.e. if character'a' is at index 2 then the character in the updated string will be 'a' + 2 = 'c'. Since value of string may go beyond 256,
4 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
Convert the ASCII value sentence to its equivalent string Given a string str which represents the ASCII (American Standard Code for Information Interchange) Sentence, the task is to convert this string into its equivalent character sequence. Examples: Input: str = "71101101107115" Output: Geeks 71, 101, 101, 107 are 115 are the unicode values of the charac
5 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