{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<h1>929. Unique Email Addresses</h1>\n", "<hr>\n", "\n", "<!--Copy Paste Leetcode statement between-->\n", "<p>Every email consists of a local name and a domain name, separated by the @ sign.</p>\n", "\n", "<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>\n", "\n", "<p>Besides lowercase letters, these emails may contain <code>'.'</code>s or <code>'+'</code>s.</p>\n", "\n", "<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>\n", "\n", "<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>\n", "\n", "<p>It is possible to use both of these rules at the same time.</p>\n", "\n", "<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>\n", "\n", "<p> </p>\n", "<p><strong>Example 1:</strong></p>\n", "<pre><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>\n", "<strong>Output: </strong><span id=\"example-output-1\">2</span>\n", "<strong><span>Explanation:</span></strong><span> \"</span><span id=\"example-input-1-1\">testemail@leetcode.com\" and \"testemail@lee.tcode.com\" </span>actually receive mails\n", "</pre>\n", "\n", "<p> </p>\n", "\n", "<p><strong>Note:</strong></p>\n", "\n", "<ul>\n", "\t<li><code>1 <= emails[i].length <= 100</code></li>\n", "\t<li><code>1 <= emails.length <= 100</code></li>\n", "\t<li>Each <code>emails[i]</code> contains exactly one <code>'@'</code> character.</li>\n", "\t<li>All local and domain names are non-empty.</li>\n", "\t<li>Local names do not start with a <code>'+'</code> character.</li>\n", "</ul>\n", "<!--Copy Paste Leetcode statement between-->\n", "\n", "<p> </p>\n", "<a href=\"https://leetcode.com/problems/unique-email-addresses/\">Source</a> \n", "<hr>\n", "\n", "<h4>Code</h4>" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def numUnique_emails(emails):\n", " seen = set()\n", " for address in emails:\n", " local, domain = address.split(\"@\")\n", " local = local.replace(\".\", \"\")\n", " local = local.split(\"+\")[0] # OR local = local[:local.find('+')] if \"+\"\n", " seen.add(local + \"@\" + domain)\n", " return len(seen)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h4>Check</h4>" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "emails = [\"test.email+alex@leetcode.com\",\"test.e.mail+bob.cathy@leetcode.com\",\"testemail+david@lee.tcode.com\"]\n", "numUnique_emails(emails)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }