VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-string-among-given-strings-represented-using-given-encryption-pattern/

⇱ Find the string among given strings represented using given encryption pattern - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the string among given strings represented using given encryption pattern

Last Updated : 23 Jul, 2025

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 string 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:


Output
PASS123word

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

Comment