<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dictionary Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/dictionary/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/dictionary/</link>
	<description></description>
	<lastBuildDate>Mon, 04 Apr 2022 02:22:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>dictionary Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/dictionary/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2227. Encrypt and Decrypt Strings</title>
		<link>https://zxi.mytechroad.com/blog/data-structure/leetcode-2227-encrypt-and-decrypt-strings/</link>
					<comments>https://zxi.mytechroad.com/blog/data-structure/leetcode-2227-encrypt-and-decrypt-strings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 03 Apr 2022 09:39:44 +0000</pubDate>
				<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[data structure]]></category>
		<category><![CDATA[dictionary]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9626</guid>

					<description><![CDATA[<p>You are given a character array&#160;keys&#160;containing&#160;unique&#160;characters and a string array&#160;values&#160;containing strings of length 2. You are also given another string array&#160;dictionary&#160;that contains all permitted original&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/data-structure/leetcode-2227-encrypt-and-decrypt-strings/">花花酱 LeetCode 2227. Encrypt and Decrypt Strings</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given a character array&nbsp;<code>keys</code>&nbsp;containing&nbsp;<strong>unique</strong>&nbsp;characters and a string array&nbsp;<code>values</code>&nbsp;containing strings of length 2. You are also given another string array&nbsp;<code>dictionary</code>&nbsp;that contains all permitted original strings after decryption. You should implement a data structure that can encrypt or decrypt a&nbsp;<strong>0-indexed</strong>&nbsp;string.</p>



<p>A string is&nbsp;<strong>encrypted</strong>&nbsp;with the following process:</p>



<ol><li>For each character&nbsp;<code>c</code>&nbsp;in the string, we find the index&nbsp;<code>i</code>&nbsp;satisfying&nbsp;<code>keys[i] == c</code>&nbsp;in&nbsp;<code>keys</code>.</li><li>Replace&nbsp;<code>c</code>&nbsp;with&nbsp;<code>values[i]</code>&nbsp;in the string.</li></ol>



<p>A string is&nbsp;<strong>decrypted</strong>&nbsp;with the following process:</p>



<ol><li>For each substring&nbsp;<code>s</code>&nbsp;of length 2 occurring at an even index in the string, we find an&nbsp;<code>i</code>&nbsp;such that&nbsp;<code>values[i] == s</code>. If there are multiple valid&nbsp;<code>i</code>, we choose&nbsp;<strong>any</strong>&nbsp;one of them. This means a string could have multiple possible strings it can decrypt to.</li><li>Replace&nbsp;<code>s</code>&nbsp;with&nbsp;<code>keys[i]</code>&nbsp;in the string.</li></ol>



<p>Implement the&nbsp;<code>Encrypter</code>&nbsp;class:</p>



<ul><li><code>Encrypter(char[] keys, String[] values, String[] dictionary)</code>&nbsp;Initializes the&nbsp;<code>Encrypter</code>&nbsp;class with&nbsp;<code>keys, values</code>, and&nbsp;<code>dictionary</code>.</li><li><code>String encrypt(String word1)</code>&nbsp;Encrypts&nbsp;<code>word1</code>&nbsp;with the encryption process described above and returns the encrypted string.</li><li><code>int decrypt(String word2)</code>&nbsp;Returns the number of possible strings&nbsp;<code>word2</code>&nbsp;could decrypt to that also appear in&nbsp;<code>dictionary</code>.</li></ul>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input</strong>
["Encrypter", "encrypt", "decrypt"]
[[['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]], ["abcd"], ["eizfeiam"]]
<strong>Output</strong>
</pre>


<p>[null, &#8220;eizfeiam&#8221;, 2]</p>



<p><strong>Explanation</strong> Encrypter encrypter = new Encrypter([[&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;], [&#8220;ei&#8221;, &#8220;zf&#8221;, &#8220;ei&#8221;, &#8220;am&#8221;], [&#8220;abcd&#8221;, &#8220;acbd&#8221;, &#8220;adbc&#8221;, &#8220;badc&#8221;, &#8220;dacb&#8221;, &#8220;cadb&#8221;, &#8220;cbda&#8221;, &#8220;abad&#8221;]); encrypter.encrypt(&#8220;abcd&#8221;); // return &#8220;eizfeiam&#8221;. &nbsp; // &#8216;a&#8217; maps to &#8220;ei&#8221;, &#8216;b&#8217; maps to &#8220;zf&#8221;, &#8216;c&#8217; maps to &#8220;ei&#8221;, and &#8216;d&#8217; maps to &#8220;am&#8221;. encrypter.decrypt(&#8220;eizfeiam&#8221;); // return 2. // &#8220;ei&#8221; can map to &#8216;a&#8217; or &#8216;c&#8217;, &#8220;zf&#8221; maps to &#8216;b&#8217;, and &#8220;am&#8221; maps to &#8216;d&#8217;. // Thus, the possible strings after decryption are &#8220;abad&#8221;, &#8220;cbad&#8221;, &#8220;abcd&#8221;, and &#8220;cbcd&#8221;. // 2 of those strings, &#8220;abad&#8221; and &#8220;abcd&#8221;, appear in dictionary, so the answer is 2.</p>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= keys.length == values.length &lt;= 26</code></li><li><code>values[i].length == 2</code></li><li><code>1 &lt;= dictionary.length &lt;= 100</code></li><li><code>1 &lt;= dictionary[i].length &lt;= 100</code></li><li>All&nbsp;<code>keys[i]</code>&nbsp;and&nbsp;<code>dictionary[i]</code>&nbsp;are&nbsp;<strong>unique</strong>.</li><li><code>1 &lt;= word1.length &lt;= 2000</code></li><li><code>1 &lt;= word2.length &lt;= 200</code></li><li>All&nbsp;<code>word1[i]</code>&nbsp;appear in&nbsp;<code>keys</code>.</li><li><code>word2.length</code>&nbsp;is even.</li><li><code>keys</code>,&nbsp;<code>values[i]</code>,&nbsp;<code>dictionary[i]</code>,&nbsp;<code>word1</code>, and&nbsp;<code>word2</code>&nbsp;only contain lowercase English letters.</li><li>At most&nbsp;<code>200</code>&nbsp;calls will be made to&nbsp;<code>encrypt</code>&nbsp;and&nbsp;<code>decrypt</code>&nbsp;<strong>in total</strong>.</li></ul>



<h2><strong>Solution: </strong></h2>



<p>For encryption, follow the instruction. Time complexity: O(len(word)) = O(2000)<br>For decryption, try all words in the dictionary and encrypt them and compare the encrypted string with the word to decrypt. Time <meta charset="utf-8">complexity: O(sum(len(word_in_dict))) = O(100*100)</p>



<p>Worst case: 200 calls to decryption, T = 200 * O(100 * 100) = O(2*10<sup>6</sup>)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Encrypter {
public:
  Encrypter(vector&lt;char&gt;&amp; keys, 
            vector&lt;string&gt;&amp; values, 
            vector&lt;string&gt;&amp; dictionary):
        vals(26),
        dict(dictionary) {
    for (size_t i = 0; i &lt; keys.size(); ++i)
      vals[keys[i] - 'a'] = values[i];  
  }

  string encrypt(string word1) {
    string ans;
    for (char c : word1)
      ans += vals[c - 'a'];
    return ans;
  }

  int decrypt(string word2) {
    return count_if(begin(dict), end(dict), [&amp;](const string&amp; w){ 
      return encrypt(w) == word2;
    });    
  }
private:
  vector&lt;string&gt; vals;
  vector&lt;string&gt; dict;
};</pre>
</div></div>



<h2><strong>Optimization</strong></h2>



<p>Pre-compute answer for all the words in dictionary.</p>



<p>decrypt: Time complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Encrypter {
public:
  Encrypter(vector&lt;char&gt;&amp; keys, 
            vector&lt;string&gt;&amp; values, 
            vector&lt;string&gt;&amp; dictionary):
        vals(26) {
    for (size_t i = 0; i &lt; keys.size(); ++i)
      vals[keys[i] - 'a'] = values[i];  
    for (const string&amp; w : dictionary)
      ++counts[encrypt(w)];
  }

  string encrypt(string word1) {
    string ans;
    for (char c : word1)
      ans += vals[c - 'a'];
    return ans;
  }

  int decrypt(string word2) {
    auto it = counts.find(word2);
    return it == counts.end() ? 0 : it-&gt;second;
  }
private:
  vector&lt;string&gt; vals;  
  unordered_map&lt;string, int&gt; counts;
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/data-structure/leetcode-2227-encrypt-and-decrypt-strings/">花花酱 LeetCode 2227. Encrypt and Decrypt Strings</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/data-structure/leetcode-2227-encrypt-and-decrypt-strings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
