1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-22 13:36:46 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2022-05-02 23:44:12 +08:00
parent 7ea03594b3
commit 2a71c78585
4790 changed files with 11696 additions and 10944 deletions

View File

@@ -2,23 +2,41 @@
"data": {
"question": {
"questionId": "1433",
"questionFrontendId": "5302",
"questionFrontendId": "2227",
"categoryTitle": "Algorithms",
"boundTopicId": 1385208,
"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>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n[&quot;Encrypter&quot;, &quot;encrypt&quot;, &quot;decrypt&quot;]\n[[[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&quot;ei&quot;, &quot;zf&quot;, &quot;ei&quot;, &quot;am&quot;], [&quot;abcd&quot;, &quot;acbd&quot;, &quot;adbc&quot;, &quot;badc&quot;, &quot;dacb&quot;, &quot;cadb&quot;, &quot;cbda&quot;, &quot;abad&quot;]], [&quot;abcd&quot;], [&quot;eizfeiam&quot;]]\n<strong>Output</strong>\n[null, &quot;eizfeiam&quot;, 2]\n\n<strong>Explanation</strong>\nEncrypter encrypter = new Encrypter([[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&quot;ei&quot;, &quot;zf&quot;, &quot;ei&quot;, &quot;am&quot;], [&quot;abcd&quot;, &quot;acbd&quot;, &quot;adbc&quot;, &quot;badc&quot;, &quot;dacb&quot;, &quot;cadb&quot;, &quot;cbda&quot;, &quot;abad&quot;]);\nencrypter.encrypt(&quot;abcd&quot;); // return &quot;eizfeiam&quot;. \n&nbsp; // &#39;a&#39; maps to &quot;ei&quot;, &#39;b&#39; maps to &quot;zf&quot;, &#39;c&#39; maps to &quot;ei&quot;, and &#39;d&#39; maps to &quot;am&quot;.\nencrypter.decrypt(&quot;eizfeiam&quot;); // return 2. \n // &quot;ei&quot; can map to &#39;a&#39; or &#39;c&#39;, &quot;zf&quot; maps to &#39;b&#39;, and &quot;am&quot; maps to &#39;d&#39;. \n // Thus, the possible strings after decryption are &quot;abad&quot;, &quot;cbad&quot;, &quot;abcd&quot;, and &quot;cbcd&quot;. \n // 2 of those strings, &quot;abad&quot; and &quot;abcd&quot;, appear in dictionary, so the answer is 2.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= keys.length == values.length &lt;= 26</code></li>\n\t<li><code>values[i].length == 2</code></li>\n\t<li><code>1 &lt;= dictionary.length &lt;= 100</code></li>\n\t<li><code>1 &lt;= dictionary[i].length &lt;= 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 &lt;= word1.length &lt;= 2000</code></li>\n\t<li><code>1 &lt;= word2.length &lt;= 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>&quot;&quot;</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>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n[&quot;Encrypter&quot;, &quot;encrypt&quot;, &quot;decrypt&quot;]\n[[[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&quot;ei&quot;, &quot;zf&quot;, &quot;ei&quot;, &quot;am&quot;], [&quot;abcd&quot;, &quot;acbd&quot;, &quot;adbc&quot;, &quot;badc&quot;, &quot;dacb&quot;, &quot;cadb&quot;, &quot;cbda&quot;, &quot;abad&quot;]], [&quot;abcd&quot;], [&quot;eizfeiam&quot;]]\n<strong>Output</strong>\n[null, &quot;eizfeiam&quot;, 2]\n\n<strong>Explanation</strong>\nEncrypter encrypter = new Encrypter([[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&quot;ei&quot;, &quot;zf&quot;, &quot;ei&quot;, &quot;am&quot;], [&quot;abcd&quot;, &quot;acbd&quot;, &quot;adbc&quot;, &quot;badc&quot;, &quot;dacb&quot;, &quot;cadb&quot;, &quot;cbda&quot;, &quot;abad&quot;]);\nencrypter.encrypt(&quot;abcd&quot;); // return &quot;eizfeiam&quot;. \n&nbsp; // &#39;a&#39; maps to &quot;ei&quot;, &#39;b&#39; maps to &quot;zf&quot;, &#39;c&#39; maps to &quot;ei&quot;, and &#39;d&#39; maps to &quot;am&quot;.\nencrypter.decrypt(&quot;eizfeiam&quot;); // return 2. \n // &quot;ei&quot; can map to &#39;a&#39; or &#39;c&#39;, &quot;zf&quot; maps to &#39;b&#39;, and &quot;am&quot; maps to &#39;d&#39;. \n // Thus, the possible strings after decryption are &quot;abad&quot;, &quot;cbad&quot;, &quot;abcd&quot;, and &quot;cbcd&quot;. \n // 2 of those strings, &quot;abad&quot; and &quot;abcd&quot;, appear in dictionary, so the answer is 2.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= keys.length == values.length &lt;= 26</code></li>\n\t<li><code>values[i].length == 2</code></li>\n\t<li><code>1 &lt;= dictionary.length &lt;= 100</code></li>\n\t<li><code>1 &lt;= dictionary[i].length &lt;= 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 &lt;= word1.length &lt;= 2000</code></li>\n\t<li><code>1 &lt;= word2.length &lt;= 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": "加密解密字符串",
"translatedContent": "<p>给你一个字符数组 <code>keys</code> ,由若干 <strong>互不相同</strong> 的字符组成。还有一个字符串数组 <code>values</code> ,内含若干长度为 2 的字符串。另给你一个字符串数组 <code>dictionary</code> ,包含解密后所有允许的原字符串。请你设计并实现一个支持加密及解密下标从 <strong>0</strong> 开始字符串的数据结构。</p>\n\n<p>字符串 <strong>加密</strong> 按下述步骤进行:</p>\n\n<ol>\n\t<li>对字符串中的每个字符 <code>c</code> ,先从 <code>keys</code> 中找出满足 <code>keys[i] == c</code> 的下标 <code>i</code> 。</li>\n\t<li>在字符串中,用&nbsp;<code>values[i]</code> 替换字符 <code>c</code> 。</li>\n</ol>\n\n<p>字符串 <strong>解密</strong> 按下述步骤进行:</p>\n\n<ol>\n\t<li>将字符串每相邻 2 个字符划分为一个子字符串,对于每个子字符串 <code>s</code> ,找出满足 <code>values[i] == s</code> 的一个下标 <code>i</code> 。如果存在多个有效的 <code>i</code> ,从中选择 <strong>任意</strong> 一个。这意味着一个字符串解密可能得到多个解密字符串。</li>\n\t<li>在字符串中,用 <code>keys[i]</code> 替换 <code>s</code> 。</li>\n</ol>\n\n<p>实现 <code>Encrypter</code> 类:</p>\n\n<ul>\n\t<li><code>Encrypter(char[] keys, String[] values, String[] dictionary)</code> 用 <code>keys</code>、<code>values</code> 和 <code>dictionary</code> 初始化 <code>Encrypter</code> 类。</li>\n\t<li><code>String encrypt(String word1)</code> 按上述加密过程完成对 <code>word1</code> 的加密,并返回加密后的字符串。</li>\n\t<li><code>int decrypt(String word2)</code> 统计并返回可以由 <code>word2</code> 解密得到且出现在 <code>dictionary</code> 中的字符串数目。</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong>示例:</strong></p>\n\n<pre>\n<strong>输入:</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>输出:</strong>\n[null, \"eizfeiam\", 2]\n\n<strong>解释:</strong>\nEncrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], [\"ei\", \"zf\", \"ei\", \"am\"], [\"abcd\", \"acbd\", \"adbc\", \"badc\", \"dacb\", \"cadb\", \"cbda\", \"abad\"]);\nencrypter.encrypt(\"abcd\"); // 返回 \"eizfeiam\"。 \n&nbsp; // 'a' 映射为 \"ei\"'b' 映射为 \"zf\"'c' 映射为 \"ei\"'d' 映射为 \"am\"。\nencrypter.decrypt(\"eizfeiam\"); // return 2. \n // \"ei\" 可以映射为 'a' 或 'c'\"zf\" 映射为 'b'\"am\" 映射为 'd'。 \n // 因此,解密后可以得到的字符串是 \"abad\"\"cbad\"\"abcd\" 和 \"cbcd\"。 \n // 其中 2 个字符串,\"abad\" 和 \"abcd\",在 dictionary 中出现,所以答案是 2 。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= keys.length == values.length &lt;= 26</code></li>\n\t<li><code>values[i].length == 2</code></li>\n\t<li><code>1 &lt;= dictionary.length &lt;= 100</code></li>\n\t<li><code>1 &lt;= dictionary[i].length &lt;= 100</code></li>\n\t<li>所有 <code>keys[i]</code> 和 <code>dictionary[i]</code> <strong>互不相同</strong></li>\n\t<li><code>1 &lt;= word1.length &lt;= 2000</code></li>\n\t<li><code>1 &lt;= word2.length &lt;= 200</code></li>\n\t<li>所有 <code>word1[i]</code> 都出现在 <code>keys</code> 中</li>\n\t<li><code>word2.length</code> 是偶数</li>\n\t<li><code>keys</code>、<code>values[i]</code>、<code>dictionary[i]</code>、<code>word1</code> 和 <code>word2</code> 只含小写英文字母</li>\n\t<li>至多调用 <code>encrypt</code> 和 <code>decrypt</code> <strong>总计</strong> <code>200</code> 次</li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 10,
"likes": 14,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false}",
"topicTags": [
{
"name": "Design",
"slug": "design",
"translatedName": "设计",
"__typename": "TopicTagNode"
},
{
"name": "Trie",
"slug": "trie",
"translatedName": "字典树",
"__typename": "TopicTagNode"
},
{
"name": "Array",
"slug": "array",
"translatedName": "数组",
"__typename": "TopicTagNode"
},
{
"name": "Hash Table",
"slug": "hash-table",
@@ -143,7 +161,7 @@
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"3.1K\", \"totalSubmission\": \"8.7K\", \"totalAcceptedRaw\": 3117, \"totalSubmissionRaw\": 8716, \"acRate\": \"35.8%\"}",
"stats": "{\"totalAccepted\": \"4.4K\", \"totalSubmission\": \"11.4K\", \"totalAcceptedRaw\": 4411, \"totalSubmissionRaw\": 11450, \"acRate\": \"38.5%\"}",
"hints": [
"For encryption, use hashmap to map each char of word1 to its value.",
"For decryption, use trie to prune when necessary."