<?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>union Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/union/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/union/</link>
	<description></description>
	<lastBuildDate>Sun, 30 Sep 2018 14:39:33 +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>union Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/union/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 916. Word Subsets</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-916-word-subsets/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-916-word-subsets/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Sep 2018 14:39:24 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[union]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4094</guid>

					<description><![CDATA[<p>Problem We are given two arrays A and B of words.  Each word is a string of lowercase letters. Now, say that word b is a subset of word a if every letter in b occurs&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-916-word-subsets/">花花酱 LeetCode 916. Word Subsets</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>We are given two arrays <code>A</code> and <code>B</code> of words.  Each word is a string of lowercase letters.</p>
<p>Now, say that word <code>b</code> is a subset of word <code>a</code><strong> </strong>if every letter in <code>b</code> occurs in <code>a</code>, <strong>including multiplicity</strong>.  For example, <code>"wrr"</code> is a subset of <code>"warrior"</code>, but is not a subset of <code>"world"</code>.</p>
<p>Now say a word <code>a</code> from <code>A</code> is <em>universal</em> if for every <code>b</code> in <code>B</code>, <code>b</code> is a subset of <code>a</code>.</p>
<p>Return a list of all universal words in <code>A</code>.  You can return the words in any order.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-1-1">["amazon","apple","facebook","google","leetcode"]</span>, B = <span id="example-input-1-2">["e","o"]</span>
<strong>Output: </strong><span id="example-output-1">["facebook","google","leetcode"]</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-2-1">["amazon","apple","facebook","google","leetcode"]</span>, B = <span id="example-input-2-2">["l","e"]</span>
<strong>Output: </strong><span id="example-output-2">["apple","google","leetcode"]</span>
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-3-1">["amazon","apple","facebook","google","leetcode"]</span>, B = <span id="example-input-3-2">["e","oo"]</span>
<strong>Output: </strong><span id="example-output-3">["facebook","google"]</span>
</pre>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-4-1">["amazon","apple","facebook","google","leetcode"]</span>, B = <span id="example-input-4-2">["lo","eo"]</span>
<strong>Output: </strong><span id="example-output-4">["google","leetcode"]</span>
</pre>
<p><strong>Example 5:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-5-1">["amazon","apple","facebook","google","leetcode"]</span>, B = <span id="example-input-5-2">["ec","oc","ceo"]</span>
<strong>Output: </strong><span id="example-output-5">["facebook","leetcode"]</span>
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= A.length, B.length &lt;= 10000</code></li>
<li><code>1 &lt;= A[i].length, B[i].length &lt;= 10</code></li>
<li><code>A[i]</code> and <code>B[i]</code> consist only of lowercase letters.</li>
<li>All words in <code>A[i]</code> are unique: there isn&#8217;t <code>i != j</code> with <code>A[i] == A[j]</code>.</li>
</ol>
<h1>Solution: Hashtable</h1>
<p>Find the max requirement for each letter from B.</p>
<p>Time complexity: O(|A|+|B|)</p>
<p>Space complexity: O(26)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 104 ms
class Solution {
public:
  vector&lt;string&gt; wordSubsets(vector&lt;string&gt;&amp; A, vector&lt;string&gt;&amp; B) {
    vector&lt;int&gt; req(26);
    for (const string&amp; b : B) {
      vector&lt;int&gt; cur(getCount(b));
      for (int i = 0; i &lt; 26; ++i)
        req[i] = max(req[i], cur[i]);
    }
    
    vector&lt;string&gt; ans;    
    for (const string&amp; a : A) {
      vector&lt;int&gt; cur(getCount(a));
      bool universal = true;
      for (int i = 0; i &lt; 26; ++i)
        if (cur[i] &lt; req[i]) {
          universal = false;
          break; 
        }
      if (universal) ans.push_back(a);
    }
    return ans;
  }
private:
  vector&lt;int&gt; getCount(const string&amp; a) {
    vector&lt;int&gt; count(26);
    for (char c : a)
      ++count[c - 'a'];
    return count;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-916-word-subsets/">花花酱 LeetCode 916. Word Subsets</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/hashtable/leetcode-916-word-subsets/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
