<?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>hashatable Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/hashatable/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/hashatable/</link>
	<description></description>
	<lastBuildDate>Sun, 28 Oct 2018 15:43:07 +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>hashatable Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/hashatable/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 929. Unique Email Addresses</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-929-unique-email-addresses/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-929-unique-email-addresses/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Oct 2018 15:30:05 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[hashatable]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4221</guid>

					<description><![CDATA[<p>Every email consists of a local name and a domain name, separated by the @ sign. For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-929-unique-email-addresses/">花花酱 LeetCode 929. Unique Email Addresses</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>Every email consists of a local name and a domain name, separated by the @ sign.</p>
<p>For example, in <code>alice@leetcode.com</code>, <code>alice</code> is the local name, and <code>leetcode.com</code> is the domain name.</p>
<p>Besides lowercase letters, these emails may contain <code>'.'</code>s or <code>'+'</code>s.</p>
<p>If you add periods (<code>'.'</code>) between some characters in the <strong>local name</strong> part of an email address, mail sent there will be forwarded to the same address without dots in the local name.  For example, <code>"alice.z@leetcode.com"</code> and <code>"alicez@leetcode.com"</code> forward to the same email address.  (Note that this rule does not apply for domain names.)</p>
<p>If you add a plus (<code>'+'</code>) in the <strong>local name</strong>, everything after the first plus sign will be <strong>ignored</strong>. This allows certain emails to be filtered, for example <code>m.y+name@email.com</code> will be forwarded to <code>my@email.com</code>.  (Again, this rule does not apply for domain names.)</p>
<p>It is possible to use both of these rules at the same time.</p>
<p>Given a list of <code>emails</code>, we send one email to each address in the list.  How many different addresses actually receive mails?</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]</span>
<strong>Output: </strong><span id="example-output-1">2</span>
<strong>Explanation:</strong> "<span id="example-input-1-1">testemail@leetcode.com" and "testemail@lee.tcode.com" </span>actually receive mails
</pre>
<p>&nbsp;</p>
<p><strong>Note:</strong></p>
<ul>
<li><code>1 &lt;= emails[i].length &lt;= 100</code></li>
<li><code>1 &lt;= emails.length &lt;= 100</code></li>
<li>Each <code>emails[i]</code> contains exactly one <code>'@'</code> character.</li>
</ul>
<p>&nbsp;</p>
<h1><strong>Solution: </strong></h1>
<p>Time complexity: O(n*l)<br />
Space complexity: O(n*l)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 24 ms
class Solution {
public:
  int numUniqueEmails(vector&lt;string&gt;&amp; emails) {
    unordered_set&lt;string&gt; s;
    for (const string&amp; email : emails) {
      string e;
      bool at = false;
      bool plus = false;
      for (char c : email) {
        if (c == '.') {
          if (!at) continue;
        } else if (c == '@') {
          at = true;          
        } else if (c == '+') {
          if (!at) {
            plus = true;
            continue;
          }
        }        
        if (!at &amp;&amp; plus) continue;
        e += c;        
      }      
      s.insert(std::move(e));
    }
    return s.size();
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-929-unique-email-addresses/">花花酱 LeetCode 929. Unique Email Addresses</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/string/leetcode-929-unique-email-addresses/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 914. X of a Kind in a Deck of Cards</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-914-x-of-a-kind-in-a-deck-of-cards/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-914-x-of-a-kind-in-a-deck-of-cards/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Sep 2018 08:02:20 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[gcd]]></category>
		<category><![CDATA[hashatable]]></category>
		<category><![CDATA[parition]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4087</guid>

					<description><![CDATA[<p>Problem n a deck of cards, each card has an integer written on it. Return true if and only if you can choose X &#62;= 2 such that it is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-914-x-of-a-kind-in-a-deck-of-cards/">花花酱 LeetCode 914. X of a Kind in a Deck of Cards</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>n a deck of cards, each card has an integer written on it.</p>
<p>Return <code>true</code> if and only if you can choose <code>X &gt;= 2</code> such that it is possible to split the entire deck into 1 or more groups of cards, where:</p>
<ul>
<li>Each group has exactly <code>X</code> cards.</li>
<li>All the cards in each group have the same integer.</li>
</ul>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">[1,2,3,4,4,3,2,1]</span>
<strong>Output: </strong><span id="example-output-1">true
<strong>Explanation</strong>: Possible partition [1,1],[2,2],[3,3],[4,4]</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">[1,1,1,2,2,2,3,3]</span>
<strong>Output: </strong><span id="example-output-2">false
</span><span id="example-output-1"><strong>Explanation</strong>: No possible partition.</span>
</pre>
<div>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">[1]</span>
<strong>Output: </strong><span id="example-output-3">false
</span><span id="example-output-1"><strong>Explanation</strong>: No possible partition.</span>
</pre>
<div>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-4-1">[1,1]</span>
<strong>Output: </strong><span id="example-output-4">true
</span><span id="example-output-1"><strong>Explanation</strong>: Possible partition [1,1]</span>
</pre>
<div>
<p><strong>Example 5:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-5-1">[1,1,2,2,2,2]</span>
<strong>Output: </strong><span id="example-output-5">true
</span><span id="example-output-1"><strong>Explanation</strong>: Possible partition [1,1],[2,2],[2,2]</span>
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= deck.length &lt;= 10000</code></li>
<li><code>0 &lt;= deck[i] &lt; 10000</code></li>
</ol>
<h1><strong>Solution 1: HashTable + Brute Force</strong></h1>
<p>Try all possible Xs. 2 ~ deck.size()</p>
<p>Time complexity: ~O(n)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 16 ms
class Solution {
public:
  bool hasGroupsSizeX(vector&lt;int&gt;&amp; deck) {
    unordered_map&lt;int, int&gt; counts;
    for (int card : deck) ++counts[card];
    for (int i = 2; i &lt;= deck.size(); ++i) {
      if (deck.size() % i) continue;
      bool ok = true;
      for (const auto&amp; pair : counts)
        if (pair.second % i) {
          ok = false;
          break;
        }
      if (ok) return true;
    }
    return false;
  }
};</pre><p></div></div></p>
<h1><strong>Solution 2: HashTable + GCD</strong></h1>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 16 ms
class Solution {
public:
  bool hasGroupsSizeX(vector&lt;int&gt;&amp; deck) {
    unordered_map&lt;int, int&gt; counts;
    for (int card : deck) ++counts[card];
    int X = deck.size();
    for (const auto&amp; p : counts) 
      X = __gcd(X, p.second);
    return X &gt;= 2;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Somebody
class Solution {
    public boolean hasGroupsSizeX(int[] deck) {
        HashMap&lt;Integer, Integer&gt; map = new HashMap&lt;Integer, Integer&gt;();
        for (int i=0; i&lt;deck.length; i++) {
            int curr = deck[i];
            if (map.containsKey(curr)) {
                map.put(curr, map.get(curr) + 1);
            } else {
                map.put(curr, 1);
            }
        }
        
        // O(nLogn)
        int gcd = 0;
        for(Integer key: map.keySet()) {
            gcd = findGCDOfTwoNumbers(map.get(key), gcd);
        }
        
        return gcd &gt;= 2;      
    }
    
    // O(Log n)
    public static int findGCDOfTwoNumbers (int a, int b) {
        if (b == 0) {
            return a;
        }
        return findGCDOfTwoNumbers(b, a%b);
    }
}</pre><p>&nbsp;</p>
</div></div>
<p><strong> </strong></p>
</div>
</div>
</div>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-914-x-of-a-kind-in-a-deck-of-cards/">花花酱 LeetCode 914. X of a Kind in a Deck of Cards</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/math/leetcode-914-x-of-a-kind-in-a-deck-of-cards/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
