|
| 1 | +<h2><a href="https://leetcode.com/problems/encrypt-and-decrypt-strings/">2227. Encrypt and Decrypt Strings</a></h2><h3>Hard</h3><hr><div><p>You are given a character array <code>keys</code> containing <strong>unique</strong> characters and a string array <code>values</code> containing strings of length 2. You are also given another string array <code>dictionary</code> that contains all permitted original strings after decryption. You should implement a data structure that can encrypt or decrypt a <strong>0-indexed</strong> string.</p> |
| 2 | + |
| 3 | +<p>A string is <strong>encrypted</strong> with the following process:</p> |
| 4 | + |
| 5 | +<ol> |
| 6 | + <li>For each character <code>c</code> in the string, we find the index <code>i</code> satisfying <code>keys[i] == c</code> in <code>keys</code>.</li> |
| 7 | + <li>Replace <code>c</code> with <code>values[i]</code> in the string.</li> |
| 8 | +</ol> |
| 9 | + |
| 10 | +<p>A string is <strong>decrypted</strong> with the following process:</p> |
| 11 | + |
| 12 | +<ol> |
| 13 | + <li>For each substring <code>s</code> of length 2 occurring at an even index in the string, we find an <code>i</code> such that <code>values[i] == s</code>. If there are multiple valid <code>i</code>, we choose <strong>any</strong> one of them. This means a string could have multiple possible strings it can decrypt to.</li> |
| 14 | + <li>Replace <code>s</code> with <code>keys[i]</code> in the string.</li> |
| 15 | +</ol> |
| 16 | + |
| 17 | +<p>Implement the <code>Encrypter</code> class:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li><code>Encrypter(char[] keys, String[] values, String[] dictionary)</code> Initializes the <code>Encrypter</code> class with <code>keys, values</code>, and <code>dictionary</code>.</li> |
| 21 | + <li><code>String encrypt(String word1)</code> Encrypts <code>word1</code> with the encryption process described above and returns the encrypted string.</li> |
| 22 | + <li><code>int decrypt(String word2)</code> Returns the number of possible strings <code>word2</code> could decrypt to that also appear in <code>dictionary</code>.</li> |
| 23 | +</ul> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong>Example 1:</strong></p> |
| 27 | + |
| 28 | +<pre><strong>Input</strong> |
| 29 | +["Encrypter", "encrypt", "decrypt"] |
| 30 | +[[['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]], ["abcd"], ["eizfeiam"]] |
| 31 | +<strong>Output</strong> |
| 32 | +[null, "eizfeiam", 2] |
| 33 | + |
| 34 | +<strong>Explanation</strong> |
| 35 | +Encrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]); |
| 36 | +encrypter.encrypt("abcd"); // return "eizfeiam". |
| 37 | + // 'a' maps to "ei", 'b' maps to "zf", 'c' maps to "ei", and 'd' maps to "am". |
| 38 | +encrypter.decrypt("eizfeiam"); // return 2. |
| 39 | + // "ei" can map to 'a' or 'c', "zf" maps to 'b', and "am" maps to 'd'. |
| 40 | + // Thus, the possible strings after decryption are "abad", "cbad", "abcd", and "cbcd". |
| 41 | + // 2 of those strings, "abad" and "abcd", appear in dictionary, so the answer is 2. |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | +<p><strong>Constraints:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>1 <= keys.length == values.length <= 26</code></li> |
| 49 | + <li><code>values[i].length == 2</code></li> |
| 50 | + <li><code>1 <= dictionary.length <= 100</code></li> |
| 51 | + <li><code>1 <= dictionary[i].length <= 100</code></li> |
| 52 | + <li>All <code>keys[i]</code> and <code>dictionary[i]</code> are <strong>unique</strong>.</li> |
| 53 | + <li><code>1 <= word1.length <= 2000</code></li> |
| 54 | + <li><code>1 <= word2.length <= 200</code></li> |
| 55 | + <li>All <code>word1[i]</code> appear in <code>keys</code>.</li> |
| 56 | + <li><code>word2.length</code> is even.</li> |
| 57 | + <li><code>keys</code>, <code>values[i]</code>, <code>dictionary[i]</code>, <code>word1</code>, and <code>word2</code> only contain lowercase English letters.</li> |
| 58 | + <li>At most <code>200</code> calls will be made to <code>encrypt</code> and <code>decrypt</code> <strong>in total</strong>.</li> |
| 59 | +</ul> |
| 60 | +</div> |
0 commit comments