mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 07:21:40 +08:00
update
This commit is contained in:
@@ -6,19 +6,25 @@
|
||||
"boundTopicId": null,
|
||||
"title": "Encrypt and Decrypt Strings",
|
||||
"titleSlug": "encrypt-and-decrypt-strings",
|
||||
"content": "<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>\n\n<p>A string is <strong>encrypted</strong> with the following process:</p>\n\n<ol>\n\t<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>\n\t<li>Replace <code>c</code> with <code>values[i]</code> in the string.</li>\n</ol>\n\n<p>A string is <strong>decrypted</strong> with the following process:</p>\n\n<ol>\n\t<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>\n\t<li>Replace <code>s</code> with <code>keys[i]</code> in the string.</li>\n</ol>\n\n<p>Implement the <code>Encrypter</code> class:</p>\n\n<ul>\n\t<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>\n\t<li><code>String encrypt(String word1)</code> Encrypts <code>word1</code> with the encryption process described above and returns the encrypted string.</li>\n\t<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>\n</ul>\n\n<p> </p>\n<p><strong>Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n["Encrypter", "encrypt", "decrypt"]\n[[['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]], ["abcd"], ["eizfeiam"]]\n<strong>Output</strong>\n[null, "eizfeiam", 2]\n\n<strong>Explanation</strong>\nEncrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]);\nencrypter.encrypt("abcd"); // return "eizfeiam". \n // 'a' maps to "ei", 'b' maps to "zf", 'c' maps to "ei", and 'd' maps to "am".\nencrypter.decrypt("eizfeiam"); // return 2. \n // "ei" can map to 'a' or 'c', "zf" maps to 'b', and "am" maps to 'd'. \n // Thus, the possible strings after decryption are "abad", "cbad", "abcd", and "cbcd". \n // 2 of those strings, "abad" and "abcd", appear in dictionary, so the answer is 2.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= keys.length == values.length <= 26</code></li>\n\t<li><code>values[i].length == 2</code></li>\n\t<li><code>1 <= dictionary.length <= 100</code></li>\n\t<li><code>1 <= dictionary[i].length <= 100</code></li>\n\t<li>All <code>keys[i]</code> and <code>dictionary[i]</code> are <strong>unique</strong>.</li>\n\t<li><code>1 <= word1.length <= 2000</code></li>\n\t<li><code>1 <= word2.length <= 200</code></li>\n\t<li>All <code>word1[i]</code> appear in <code>keys</code>.</li>\n\t<li><code>word2.length</code> is even.</li>\n\t<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>\n\t<li>At most <code>200</code> calls will be made to <code>encrypt</code> and <code>decrypt</code> <strong>in total</strong>.</li>\n</ul>\n",
|
||||
"content": "<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>\n\n<p>A string is <strong>encrypted</strong> with the following process:</p>\n\n<ol>\n\t<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>\n\t<li>Replace <code>c</code> with <code>values[i]</code> in the string.</li>\n</ol>\n\n<p>Note that in case a character of the string is <strong>not present</strong> in <code>keys</code>, the encryption process cannot be carried out, and an empty string <code>""</code> is returned.</p>\n\n<p>A string is <strong>decrypted</strong> with the following process:</p>\n\n<ol>\n\t<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>\n\t<li>Replace <code>s</code> with <code>keys[i]</code> in the string.</li>\n</ol>\n\n<p>Implement the <code>Encrypter</code> class:</p>\n\n<ul>\n\t<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>\n\t<li><code>String encrypt(String word1)</code> Encrypts <code>word1</code> with the encryption process described above and returns the encrypted string.</li>\n\t<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>\n</ul>\n\n<p> </p>\n<p><strong>Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n["Encrypter", "encrypt", "decrypt"]\n[[['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]], ["abcd"], ["eizfeiam"]]\n<strong>Output</strong>\n[null, "eizfeiam", 2]\n\n<strong>Explanation</strong>\nEncrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]);\nencrypter.encrypt("abcd"); // return "eizfeiam". \n // 'a' maps to "ei", 'b' maps to "zf", 'c' maps to "ei", and 'd' maps to "am".\nencrypter.decrypt("eizfeiam"); // return 2. \n // "ei" can map to 'a' or 'c', "zf" maps to 'b', and "am" maps to 'd'. \n // Thus, the possible strings after decryption are "abad", "cbad", "abcd", and "cbcd". \n // 2 of those strings, "abad" and "abcd", appear in dictionary, so the answer is 2.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= keys.length == values.length <= 26</code></li>\n\t<li><code>values[i].length == 2</code></li>\n\t<li><code>1 <= dictionary.length <= 100</code></li>\n\t<li><code>1 <= dictionary[i].length <= 100</code></li>\n\t<li>All <code>keys[i]</code> and <code>dictionary[i]</code> are <strong>unique</strong>.</li>\n\t<li><code>1 <= word1.length <= 2000</code></li>\n\t<li><code>1 <= word2.length <= 200</code></li>\n\t<li>All <code>word1[i]</code> appear in <code>keys</code>.</li>\n\t<li><code>word2.length</code> is even.</li>\n\t<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>\n\t<li>At most <code>200</code> calls will be made to <code>encrypt</code> and <code>decrypt</code> <strong>in total</strong>.</li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Hard",
|
||||
"likes": 90,
|
||||
"dislikes": 12,
|
||||
"likes": 186,
|
||||
"dislikes": 35,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Implement Trie (Prefix Tree)\", \"titleSlug\": \"implement-trie-prefix-tree\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Word Search II\", \"titleSlug\": \"word-search-ii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Implement Trie II (Prefix Tree)\", \"titleSlug\": \"implement-trie-ii-prefix-tree\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "[\"Encrypter\",\"encrypt\",\"decrypt\"]\n[[[\"a\",\"b\",\"c\",\"d\"],[\"ei\",\"zf\",\"ei\",\"am\"],[\"abcd\",\"acbd\",\"adbc\",\"badc\",\"dacb\",\"cadb\",\"cbda\",\"abad\"]],[\"abcd\"],[\"eizfeiam\"]]",
|
||||
"categoryTitle": "Algorithms",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Array",
|
||||
"slug": "array",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
},
|
||||
{
|
||||
"name": "Hash Table",
|
||||
"slug": "hash-table",
|
||||
@@ -30,6 +36,18 @@
|
||||
"slug": "string",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
},
|
||||
{
|
||||
"name": "Design",
|
||||
"slug": "design",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
},
|
||||
{
|
||||
"name": "Trie",
|
||||
"slug": "trie",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
@@ -143,7 +161,7 @@
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"4.2K\", \"totalSubmission\": \"12.2K\", \"totalAcceptedRaw\": 4188, \"totalSubmissionRaw\": 12160, \"acRate\": \"34.4%\"}",
|
||||
"stats": "{\"totalAccepted\": \"7K\", \"totalSubmission\": \"18.4K\", \"totalAcceptedRaw\": 7017, \"totalSubmissionRaw\": 18443, \"acRate\": \"38.0%\"}",
|
||||
"hints": [
|
||||
"For encryption, use hashmap to map each char of word1 to its value.",
|
||||
"For decryption, use trie to prune when necessary."
|
||||
|
Reference in New Issue
Block a user