Open In App

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 length of each string in the array is at least 3 and if there is more than one correct answer, print among them.



Examples: 

Input: arr[] = {“P@sswORD1”, “PASS123word”}, str = “4dro6”
Output: PASS123word
Explanation: The decrypted string representing str = “4dro6” should have 
4 upper case letters, sum of all digits in it as 6 and ends with “ord”. 
The output string satisfies all the following properties.



Input: arr[] = {“Geeks”, “code”, “Day&Night”}, str = “1thg10”
Output: -1
Explanation: No such string exists which satisfies the encryption.

 

Approach: The given problem is an implementation based problem that can be solved by following the below steps:

Below is the implementation of the above approach:




// C++ Program of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the correct decrypted
// string out of the given array arr
string decryptStr(vector<string> arr,
                  string str)
{
    // Stores the size of string
    int N = str.size();
    int i = 0, j = N - 1;
 
    // Last index of the starting digit
    while (isdigit(str[i]))
        i++;
 
    // First index of the last string
    while (isdigit(str[j]))
        j--;
 
    // Stores the starting integer
    int start = stoi(str.substr(0, i));
 
    // Stores the ending integer
    int end = stoi(str.substr(j + 1, N - j));
 
    // Stores the middle string
    string mid = str.substr(i, 3);
 
    // Reverse the middle string
    reverse(mid.begin(), mid.end());
 
    // Loop to iterate over all
    // string in the given array
    for (auto S : arr) {
        int upper = 0, sum = 0;
        for (int i = 0; i < S.length(); i++) {
 
            // Calculate the count
            // of upper case char
            if (isupper(S[i]))
                upper++;
 
            // Calculate the sum
            // of digits in S
            if (isdigit(S[i]))
                sum += (S[i] - '0');
        }
        // If all the required conditions
        // are satisfied
        if (upper == start && sum == end
            && S.substr(S.length() - 3, 3)
            == mid)
            return S;
    }
 
    return "-1";
}
 
// Driver Code
int main()
{
    vector<string> arr = { "P@sswORD1",
                          "PASS123word" };
    string str = "4dro6";
 
    cout << decryptStr(arr, str);
    return 0;
}




// Java Program of the above approach
import java.util.*;
class GFG{
 
// Function to find the correct decrypted
// String out of the given array arr
static String decryptStr(String []arr,
                  String str)
{
   
    // Stores the size of String
    int N = str.length();
    int i = 0, j = N - 1;
 
    // Last index of the starting digit
    while (Character.isDigit(str.charAt(i)))
        i++;
 
    // First index of the last String
    while (Character.isDigit(str.charAt(j)))
        j--;
 
    // Stores the starting integer
    int start = Integer.valueOf(str.substring(0, i));
 
    // Stores the ending integer
    int end = Integer.valueOf(str.substring(j + 1));
 
    // Stores the middle String
    String mid = str.substring(i, i+3);
 
    // Reverse the middle String
    mid = reverse(mid);
 
    // Loop to iterate over all
    // String in the given array
    for (String S : arr) {
        int upper = 0, sum = 0;
        for (int i2 = 0; i2 < S.length(); i2++) {
 
            // Calculate the count
            // of upper case char
            if (Character.isUpperCase(S.charAt(i2)))
                upper++;
 
            // Calculate the sum
            // of digits in S
            if (Character.isDigit(S.charAt(i2)))
                sum += (S.charAt(i2) - '0');
        }
        // If all the required conditions
        // are satisfied
        if (upper == start && sum == end
            && S.substring(S.length() - 3).equals(mid))
            return S;
    }
 
    return "-1";
}
static String reverse(String input) {
    char[] a = input.toCharArray();
    int l, r = a.length - 1;
    for (l = 0; l < r; l++, r--) {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.valueOf(a);
}
   
// Driver Code
public static void main(String[] args)
{
    String []arr = { "P@sswORD1",
                          "PASS123word" };
    String str = "4dro6";
 
    System.out.print(decryptStr(arr, str));
}
}
 
// This code is contributed by shikhasingrajput




# Python 3 Program of the above approach
 
# Function to find the correct decrypted
# string out of the given array arr
def decryptStr(arr,
               st):
 
    # Stores the size of string
    N = len(st)
    i = 0
    j = N - 1
 
    # Last index of the starting digit
    while ((st[i].isdigit())):
        i += 1
 
    # First index of the last string
    while ((st[j].isdigit())):
        j -= 1
 
    # Stores the starting integer
    start = int(st[0:i])
 
    # Stores the ending integer
    end = int(st[j + 1:])
 
    # Stores the middle string
    mid = st[i:3+i]
 
    # Reverse the middle string
    mid_list = list(mid)
    mid_list.reverse()
    mid = ''.join(mid_list)
 
    # Loop to iterate over all
    # string in the given array
    for S in arr:
        upper = 0
        sum = 0
        for i in range(len(S)):
 
            # Calculate the count
            # of upper case char
            if ((S[i].isupper())):
                upper += 1
 
            # Calculate the sum
            # of digits in S
            if ((S[i].isdigit())):
                sum += (ord(S[i]) - ord('0'))
 
        # If all the required conditions
        # are satisfied
        if (upper == start and sum == end
            and S[len(S) - 3:]
                == mid):
            return S
 
    return "-1"
 
# Driver Code
if __name__ == "__main__":
 
    arr = ["P@sswORD1",
           "PASS123word"]
    st = "4dro6"
 
    print(decryptStr(arr, st))
 
    # This code is contributed by ukasp.




// C# program to implement above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to find the correct decrypted
  // String out of the given array arr
  static String decryptStr(String []arr, String str)
  {
 
    // Stores the size of String
    int N = str.Length;
    int i = 0, j = N - 1;
 
    // Last index of the starting digit
    while (Char.IsDigit(str[i]))
      i++;
 
    // First index of the last String
    while (Char.IsDigit(str[j]))
      j--;
 
 
    // Stores the starting integer
    int start = Convert.ToInt32(str.Substring(0, i));
 
    // Stores the ending integer
    int end = Convert.ToInt32(str.Substring(j+1));
 
    // Stores the middle String
    String mid = str.Substring(i, 3);
 
    // Reverse the middle String
    mid = reverse(mid);
 
    // Loop to iterate over all
    // String in the given array
    foreach (String S in arr) {
      int upper = 0, sum = 0;
      for (int i2 = 0 ; i2 < S.Length ; i2++) {
 
        // Calculate the count
        // of upper case char
        if (Char.IsUpper(S[i2]))
          upper++;
 
        // Calculate the sum
        // of digits in S
        if (Char.IsDigit(S[i2]))
          sum += ((int)S[i2] - (int)'0');
      }
      // If all the required conditions
      // are satisfied
      if (upper == start && sum == end && S.Substring(S.Length - 3).Equals(mid))
        return S;
    }
 
    return "-1";
  }
 
  static String reverse(String input) {
    char[] a = input.ToCharArray();
    int l, r = a.Length - 1;
    for (l = 0 ; l < r ; l++, r--) {
      char temp = a[l];
      a[l] = a[r];
      a[r] = temp;
    }
    return string.Join("", a);
  }
 
  // Driver code
  public static void Main(string[] args){
 
    String []arr = new String[]{"P@sswORD1", "PASS123word"};
    String str = "4dro6";
 
    Console.WriteLine(decryptStr(arr, str));
 
  }
}
 
// This code is contributed by subhamgoyal2014.




<script>
    // JavaScript Program of the above approach
 
    // Function to find the correct decrypted
    // string out of the given array arr
    const decryptStr = (arr, str) => {
     
        // Stores the size of string
        let N = str.length;
        let i = 0, j = N - 1;
 
        // Last index of the starting digit
        while (str[i] >= '0' && str[i] <= '9')
            i++;
 
        // First index of the last string
        while (str[j] >= '0' && str[j] <= '9')
            j--;
        // Stores the starting integer
        let start = parseInt(str.substring(0, i));
 
        // Stores the ending integer
        let end = parseInt(str.substring(j + 1, j + 1 + N - j));
 
        // Stores the middle string
        let mid = str.substring(i, i + 3);
        // Reverse the middle string
        mid = [...mid].reverse().join('')
 
        // Loop to iterate over all
        // string in the given array
        for (let S in arr) {
            let upper = 0, sum = 0;
            for (let i = 0; i < arr[S].length; i++) {
 
                // Calculate the count
                // of upper case char
                if (arr[S][i] >= 'A' && arr[S][i] <= 'Z')
                    upper++;
 
                // Calculate the sum
                // of digits in S
                if (arr[S][i] >= '0' && arr[S][i] <= '9')
                    sum += (arr[S].charCodeAt(i) - '0'.charCodeAt(0));
            }
            // If all the required conditions
            // are satisfied
            if (upper == start && sum == end
                && arr[S].substring(arr[S].length - 3, arr[S].length + 3)
                == mid)
                return arr[S];
        }
 
        return "-1";
    }
 
    // Driver Code
    let arr = ["P@sswORD1", "PASS123word"];
    let str = "4dro6";
 
    document.write(decryptStr(arr, str));
 
// This code is contributed by rakeshsahni
 
</script>

Output
PASS123word

Time Complexity: O(N * M) where M is the maximum length of a string of the array
Auxiliary Space: O(1)


Article Tags :