📌  相关文章
📜  在使用给定加密模式表示的给定字符串中查找字符串

📅  最后修改于: 2022-05-13 01:56:08.920000             🧑  作者: Mango

在使用给定加密模式表示的给定字符串中查找字符串

给定一个大小为N的字符串数组arr[]和一个加密字符串str ,任务是从给定的字符串数组中找到正确的字符串,其加密将给出str其中str使用以下规则加密:

  • 起始字符形成一个整数,表示解密字符串中大写符号的数量。
  • 接下来的 3 个字符是解密字符串的最后 3 个字符,以相反的顺序排列。
  • 最后几个字符也形成一个整数,表示密码中所有数字的总和。

数组中每个字符串的长度至少为 3,如果有多个正确答案,则打印其中。

例子:

方法:给定的问题是一个基于实现的问题,可以通过以下步骤解决:

  • 将由起始数字表示的整数存储在整数开始中。
  • 类似地,将由最后一位数字表示的整数存储在整数末尾。
  • 存储和反转字符串中给定的两个整数中间的字符串。
  • 遍历给定数组arr[]中的所有字符串,如果其中任何一个满足所有给定的三个条件,则返回相应的字符串,否则返回-1

下面是上述方法的实现:

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


输出
PASS123word

时间复杂度: O(N * M) 其中 M 是数组字符串的最大长度
辅助空间: O(1)