2227. Encrypt and Decrypt Strings Hard
1/**
2 * [2227] Encrypt and Decrypt Strings
3 *
4 * You are given a character array keys containing unique characters and a string array values containing strings of length 2. You are also given another string array dictionary that contains all permitted original strings after decryption. You should implement a data structure that can encrypt or decrypt a 0-indexed string.
5 * A string is encrypted with the following process:
6 * <ol>
7 * For each character c in the string, we find the index i satisfying keys[i] == c in keys.
8 * Replace c with values[i] in the string.
9 * </ol>
10 * Note that in case a character of the string is not present in keys, the encryption process cannot be carried out, and an empty string "" is returned.
11 * A string is decrypted with the following process:
12 * <ol>
13 * For each substring s of length 2 occurring at an even index in the string, we find an i such that values[i] == s. If there are multiple valid i, we choose any one of them. This means a string could have multiple possible strings it can decrypt to.
14 * Replace s with keys[i] in the string.
15 * </ol>
16 * Implement the Encrypter class:
17 *
18 * Encrypter(char[] keys, String[] values, String[] dictionary) Initializes the Encrypter class with keys, values, and dictionary.
19 * String encrypt(String word1) Encrypts word1 with the encryption process described above and returns the encrypted string.
20 * int decrypt(String word2) Returns the number of possible strings word2 could decrypt to that also appear in dictionary.
21 *
22 *
23 * Example 1:
24 *
25 * Input
26 * ["Encrypter", "encrypt", "decrypt"]
27 * [[['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]], ["abcd"], ["eizfeiam"]]
28 * Output
29 * [null, "eizfeiam", 2]
30 * Explanation
31 * Encrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]);
32 * encrypter.encrypt("abcd"); // return "eizfeiam".
33 * // 'a' maps to "ei", 'b' maps to "zf", 'c' maps to "ei", and 'd' maps to "am".
34 * encrypter.decrypt("eizfeiam"); // return 2.
35 * // "ei" can map to 'a' or 'c', "zf" maps to 'b', and "am" maps to 'd'.
36 * // Thus, the possible strings after decryption are "abad", "cbad", "abcd", and "cbcd".
37 * // 2 of those strings, "abad" and "abcd", appear in dictionary, so the answer is 2.
38 *
39 *
40 * Constraints:
41 *
42 * 1 <= keys.length == values.length <= 26
43 * values[i].length == 2
44 * 1 <= dictionary.length <= 100
45 * 1 <= dictionary[i].length <= 100
46 * All keys[i] and dictionary[i] are unique.
47 * 1 <= word1.length <= 2000
48 * 1 <= word2.length <= 200
49 * All word1[i] appear in keys.
50 * word2.length is even.
51 * keys, values[i], dictionary[i], word1, and word2 only contain lowercase English letters.
52 * At most 200 calls will be made to encrypt and decrypt in total.
53 *
54 */
55pub struct Solution {}
56
57// problem: https://leetcode.com/problems/encrypt-and-decrypt-strings/
58// discuss: https://leetcode.com/problems/encrypt-and-decrypt-strings/discuss/?currentPage=1&orderBy=most_votes&query=
59
60// submission codes start here
61
62struct Encrypter {
63 false
64 }
65
66
67/**
68 * `&self` means the method takes an immutable reference.
69 * If you need a mutable reference, change it to `&mut self` instead.
70 */
71impl Encrypter {
72
73 fn new(keys: Vec<char>, values: Vec<String>, dictionary: Vec<String>) -> Self {
74
75 }
76
77 fn encrypt(&self, word1: String) -> String {
78
79 }
80
81 fn decrypt(&self, word2: String) -> i32 {
82
83 }
84}
85
86/**
87 * Your Encrypter object will be instantiated and called as such:
88 * let obj = Encrypter::new(keys, values, dictionary);
89 * let ret_1: String = obj.encrypt(word1);
90 * let ret_2: i32 = obj.decrypt(word2);
91 */
92
93// submission codes end
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn test_2227() {
101 }
102}
103
Back
© 2025 bowen.ge All Rights Reserved.