Find the String Among given Strings Represented using given Encryption Pattern


In this problem, we need to find the original string from the encrypted string using the given encryption rules. We can get the original string if we use the encryption rules in reverse order.

Problem statement – We have given an encrypted string. Also, we have given an array of strings containing multiple strings. We need to find the string from the array, so if we encrypt according to the below rules, we can get the encrypted string.

Encryption rules

  • The encrypted string should start with a positive integer, representing a number of uppercase letters in the original string.

  • After the positive integer, the string should contain the last 3 characters of the original string in the reverse order.

  • At last, the string should contain a positive integer, representing the sum of all digits of the string.

Sample examples

Input

str_array = {"ABXC237klg", "esre45Qewr3"}, enc_str = "4glk12"

Output

ABXC237klg

Explanation – Let’s decrypt the enc_str string.

  • 4 – The string should contain 4 uppercase characters.

  • glk – The last 3 characters of the original string in the reverse order.

  • 12 – The sum of all string digits is (2 + 3 + 7 = 12).

So, the output string follows all encryption rules.

Input

str_array = {"ABXC237klp", "esre45Qewr3"}, enc_str = "4glk12"

Output

-1

Explanation – The array doesn’t contain a decrypted string of the enc_str string.

Input

str_array = {"JK253Der", "P5Q32mnDer", "GHD23fgd4"}; enc_str = "3reD10";

Output

JK253Der

Explanation – If we encrypt the ‘JK253Der’ or “P5Q32mnDer” string, we get the "3reD10" encrypted string. So we can print any of the two in the output.

Approach 1

This approach will use the encryption rules for a given string. First, we will extract the starting integer and check if the string contains the total number of uppercase characters. After that, we will extract three characters and check whether the array string contains all 3 characters at last in the reverse order. At last, we will extract the last integer of the encrypted string and check whether it is equal to the sum of all digits of the string.

Algorithm

Step 1 – Initialize the ‘p’ variable with 0 and the ‘q’ variable with string length -1.

Step 2 – Use the while loop, and increment the value of ‘p’ until we get the alphabetical character in the string to find the starting integer value.

Step 3 – Again, use the while loop, and decrement the value of ‘q’ until we get the alphabetical character in the string to find the ending integer value.

Step 4 – Use the substr() method to take positive and ending integer strings using the p and q values. Also, use the stoi() method to convert the string to a number and store it in the ‘initial’ and ‘ending’ variables.

Step 5 – Get the 3 middle characters of the string using the substr() method, and use the reverse() method to reverse them.

Step 6 – Start traversing the string array, and initialize the ‘upperCase’ and ‘digitSum’ variable with 0 to store total number of uppercase characters and the sum of digits of a particular string.

Step 7 – Use the nested loop to traverse the string. Use the isUpper() method to check whether the current character is in uppercase. If yes, increase the value of the ‘upperCase’ variable by 1.

Step 8 – Use the isdigit() method to check whether the current character is digitized. If yes, add the digit value to the ‘digitSum’ variable.

Step 9 – If the value of the ‘upperCase’ variable is equal to the ‘initial’, ‘digitSum’ is equal to the ‘ending’, and the last 3 characters of the string are equal to the ‘middle’, return the string.

Step 10 – Finally, return ‘-1’ if we haven’t found any string.

Example

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

string getOriginalStr(vector<string> str_array, string enc_str) {
    // String size
    int len = enc_str.size();
    int p = 0, q = len - 1;
    // Getting the digit's last index from the start
    while (isdigit(enc_str[p]))
        p++;
    // Gettig the character's first index from the end
    while (isdigit(enc_str[q]))
        q--;
    // Get initial integer
    int initial = stoi(enc_str.substr(0, p));
    // Get the ending integer
    int ending = stoi(enc_str.substr(q + 1, len - q));
    // Get middle string
    string middle = enc_str.substr(p, 3);
    // Reverse the string
    reverse(middle.begin(), middle.end());
    // Traverse array of string
    for (auto temp_str : str_array) {
        int upperCase = 0, digitsSum = 0;
        // Traverse string
        for (int p = 0; p < temp_str.length(); p++) {
            // For uppercase character
            if (isupper(temp_str[p]))
                upperCase++;
            // Get the sum of the present digits
            if (isdigit(temp_str[p]))
                digitsSum += (temp_str[p] - '0');
        }
        // Check for all conditions
        if (upperCase == initial && digitsSum == ending && temp_str.substr(temp_str.length() - 3, 3) == middle)
            return temp_str;
    }
    return "-1";
}
int main() {
    vector<string> str_array = {"ABXC237klg", "esre45Qewr3"};
    string enc_str = "4glk12";
    cout << "The original string is - " << getOriginalStr(str_array, enc_str);
    return 0;
}

Output

The original string is - ABXC237klg

Time complexity – O(P*Q) where P is the length of the array, and Q is the maximum length of the string.

Space complexity – O(1) as we use the constant space.

Whenever we require to solve a similar problem, we should check strings that follow the given rules and generate the encrypted string as an output.

Updated on: 25-Aug-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements