# Find the string among given strings represented using given encryption pattern

• Last Updated : 07 Feb, 2022

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 symbols In the decrypted string.
• The next 3 characters are the last 3 characters of the decrypted string in reverse order.
• The last few characters also form an integer representing the sum of all digits in the password.

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:

• Store the integer represented by the starting digits in an integer start.
• Similarly, store the integer represented by the last digits in an integer end.
• Store and reverse the string in the middle of the given two integers in a sting mid.
• Iterate over all the strings in the given array arr[] and if any of them satisfies all the given three conditions, return the respective string, otherwise, return -1.

Below is the implementation of the above approach:

## C++

 `// C++ Program of the above approach``#include ``using` `namespace` `std;` `// Function to find the correct decrypted``// string out of the given array arr``string decryptStr(vector 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 arr = { ``"P@sswORD1"``,``                          ``"PASS123word"` `};``    ``string str = ``"4dro6"``;` `    ``cout << decryptStr(arr, str);``    ``return` `0;``}`

## Java

 `// 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`

## Python3

 `# 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.`

## Javascript

 ``
Output
`PASS123word`

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

My Personal Notes arrow_drop_up