{"id":4221,"date":"2018-10-28T08:30:05","date_gmt":"2018-10-28T15:30:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4221"},"modified":"2018-10-28T08:43:07","modified_gmt":"2018-10-28T15:43:07","slug":"leetcode-929-unique-email-addresses","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-929-unique-email-addresses\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 929. Unique Email Addresses"},"content":{"rendered":"<p>Every email consists of a local name and a domain name, separated by the @ sign.<\/p>\n<p>For example, in\u00a0<code>alice@leetcode.com<\/code>,\u00a0<code>alice<\/code>\u00a0is the local name, and\u00a0<code>leetcode.com<\/code>\u00a0is the domain name.<\/p>\n<p>Besides lowercase letters, these emails may contain\u00a0<code>'.'<\/code>s or\u00a0<code>'+'<\/code>s.<\/p>\n<p>If you add periods (<code>'.'<\/code>) between some characters in the\u00a0<strong>local name<\/strong>\u00a0part of an email address, mail sent there will be forwarded to the same address without dots in the local name.\u00a0 For example,\u00a0<code>\"alice.z@leetcode.com\"<\/code>\u00a0and\u00a0<code>\"alicez@leetcode.com\"<\/code>\u00a0forward to the same email address.\u00a0 (Note that this rule does not apply for domain names.)<\/p>\n<p>If you add a plus (<code>'+'<\/code>) in the\u00a0<strong>local name<\/strong>, everything after the first plus sign will be\u00a0<strong>ignored<\/strong>. This allows certain emails to be filtered, for example\u00a0<code>m.y+name@email.com<\/code>\u00a0will be forwarded to\u00a0<code>my@email.com<\/code>.\u00a0 (Again, this rule does not apply for domain names.)<\/p>\n<p>It is possible to use both of these rules at the same time.<\/p>\n<p>Given a list of\u00a0<code>emails<\/code>, we send one email to each address in the list.\u00a0\u00a0How many different addresses actually receive mails?<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<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>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">2<\/span>\r\n<strong>Explanation:<\/strong>\u00a0\"<span id=\"example-input-1-1\">testemail@leetcode.com\" and \"testemail@lee.tcode.com\" <\/span>actually receive mails\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= emails[i].length\u00a0&lt;= 100<\/code><\/li>\n<li><code>1 &lt;= emails.length &lt;= 100<\/code><\/li>\n<li>Each\u00a0<code>emails[i]<\/code>\u00a0contains exactly one\u00a0<code>'@'<\/code>\u00a0character.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h1><strong>Solution:\u00a0<\/strong><\/h1>\n<p>Time complexity: O(n*l)<br \/>\nSpace complexity: O(n*l)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, 24 ms\r\nclass Solution {\r\npublic:\r\n  int numUniqueEmails(vector&lt;string&gt;&amp; emails) {\r\n    unordered_set&lt;string&gt; s;\r\n    for (const string&amp; email : emails) {\r\n      string e;\r\n      bool at = false;\r\n      bool plus = false;\r\n      for (char c : email) {\r\n        if (c == '.') {\r\n          if (!at) continue;\r\n        } else if (c == '@') {\r\n          at = true;          \r\n        } else if (c == '+') {\r\n          if (!at) {\r\n            plus = true;\r\n            continue;\r\n          }\r\n        }        \r\n        if (!at &amp;&amp; plus) continue;\r\n        e += c;        \r\n      }      \r\n      s.insert(std::move(e));\r\n    }\r\n    return s.size();\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Every email consists of a local name and a domain name, separated by the @ sign. For example, in\u00a0alice@leetcode.com,\u00a0alice\u00a0is the local name, and\u00a0leetcode.com\u00a0is the domain&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,47],"tags":[222,426,418,4],"class_list":["post-4221","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-easy","tag-email","tag-hashatable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4221"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4221\/revisions"}],"predecessor-version":[{"id":4225,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4221\/revisions\/4225"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}