From 272185ecf6ebb3c9d6fe48348741bd9fb144efd9 Mon Sep 17 00:00:00 2001 From: busker <165013324+hiroki-horiguchi-dev@users.noreply.github.com> Date: Fri, 1 May 2026 17:42:29 +0900 Subject: [PATCH 1/4] Create 929.md --- hashmap/929.md | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 hashmap/929.md diff --git a/hashmap/929.md b/hashmap/929.md new file mode 100644 index 0000000..bab0905 --- /dev/null +++ b/hashmap/929.md @@ -0,0 +1,72 @@ +- 問題: [929. Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/description/) +- コメント集: [929. Unique Email Addresses](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.7b7phcxky0ug) +- 条件 + - `1 <= emails.length <= 100` + - `1 <= emails[i].length <= 100` + - `emails[i] consist of lowercase English letters, '+', '.' and '@'.` + - `Each emails[i] contains exactly one '@' character.` + - `All local and domain names are non-empty.` + - `Local names do not start with a '+' character.` + - `Domain names end with the ".com" suffix.` + - `Domain names must contain at least one character before ".com" suffix.` +- 方針 + - @ の前(local name)と後(domain name)で split する + - local name に対して処理を実行 + - local name と domain name を連結する + - Set に詰める + - Set のサイズを返却する +- 時間 + - 方針を考えるところまで3分 +- 時間計算量: `O(N)` +- 空間計算量: `O(N)` +- 初期方針 +```java +class Solution { + public int numUniqueEmails(String[] emails) { + Set uniqueEmails = new HashSet<>(); + for (String email : emails) { + // split at @ index. + + // remove dots + + // remove after + symbol + + // add unique email to uniqueEmails(Set) + uniqueEmails.add(formattedEmail); + } + + return uniqueEmails.size(); + } +} +``` + +## 1st +- replace vs replaceAll を間違えた +- split を使わずに indexOf を使ったので少し冗長なコードになった +```java +class Solution { + public int numUniqueEmails(String[] emails) { + Set uniqueEmails = new HashSet<>(); + for (String email : emails) { + // split at @ index. + String[] pair = email.split("@"); + String localName = pair[0]; + String domainName = pair[1]; + + // remove after + symbol + int plusSymbolIndex = localName.indexOf("+"); + if (plusSymbolIndex != -1) { + localName = localName.substring(0, plusSymbolIndex); + } + + // remove dots + localName = localName.replace(".", ""); + + // add unique email to uniqueEmails(Set) + uniqueEmails.add(localName + "@" + domainName); + } + + return uniqueEmails.size(); + } +} +``` \ No newline at end of file From 955e30246057becf660deec7d3842a257ce28bb0 Mon Sep 17 00:00:00 2001 From: busker <165013324+hiroki-horiguchi-dev@users.noreply.github.com> Date: Fri, 1 May 2026 18:14:21 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=82=92=E8=AA=AD=E3=82=93=E3=81=A0=E7=B5=90=E6=9E=9C=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E8=A8=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashmap/929.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hashmap/929.md b/hashmap/929.md index bab0905..f698954 100644 --- a/hashmap/929.md +++ b/hashmap/929.md @@ -1,5 +1,12 @@ - 問題: [929. Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/description/) - コメント集: [929. Unique Email Addresses](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.7b7phcxky0ug) + - [ユースケースの想定](https://github.com/Yoshiki-Iwasa/Arai60/pull/13#discussion_r1649832719) + - これはなるほどそうだなと思う。 + - 重複するようなメアドを何かに登録するタイミングで、ユニークなメアド件数を知りたい場合があまり考えつかない。。 + - [ここ](https://github.com/Yoshiki-Iwasa/Arai60/pull/13#discussion_r1676820615)で議論されているようなユースケースかあ、うーんと思うなどした。 + - [ユースケースの想定2](https://github.com/syoshida20/leetcode/pull/20#discussion_r2079714768) + - データサイエンス目的の処理内で使われているバッチであれば落ちてほしい、なるほどそうかと思った + - [rfc3.4.1を読んでみるなどした](https://www.rfc-editor.org/rfc/rfc5322.html#section-3.4.1) - 条件 - `1 <= emails.length <= 100` - `1 <= emails[i].length <= 100` @@ -11,7 +18,7 @@ - `Domain names must contain at least one character before ".com" suffix.` - 方針 - @ の前(local name)と後(domain name)で split する - - local name に対して処理を実行 + - local name に対して2つの処理を実行 - local name と domain name を連結する - Set に詰める - Set のサイズを返却する @@ -43,6 +50,7 @@ class Solution { ## 1st - replace vs replaceAll を間違えた - split を使わずに indexOf を使ったので少し冗長なコードになった +- 初期方針で立てていた、「remove dots」「remove after + symbol」の順序は逆の方が計算回数が少なくて済む ```java class Solution { public int numUniqueEmails(String[] emails) { From 00c42bd22f66913550e2ed50fd37dd63f679bb5c Mon Sep 17 00:00:00 2001 From: busker <165013324+hiroki-horiguchi-dev@users.noreply.github.com> Date: Tue, 5 May 2026 14:11:14 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashmap/929.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/hashmap/929.md b/hashmap/929.md index f698954..669316d 100644 --- a/hashmap/929.md +++ b/hashmap/929.md @@ -77,4 +77,16 @@ class Solution { return uniqueEmails.size(); } } -``` \ No newline at end of file +``` + +## いただいたコメントから要点を整理 +- https://github.com/akmhmgc/arai60/pull/11#discussion_r2311995496 + 1. RFC 5322を知っているか? -> メールアドレスには公式仕様があり、ローカルパートに ' や " が使えるなど、想像以上に複雑だという認識があるか。 + 2. 正規表現を知っているか + -> 単純な文字列操作だけでなく、パターンマッチの道具として正規表現を持っているか。 + 3. 正規表現での完全な検証が困難だと知っているか + ->「99%カバーできるが完璧はない」というトレードオフの話ができるか。 + 4. フォローアップ:バリデーションが本当に必要な場面は? + - 必要性が低い場面 → 社内ツールで入力者が信頼できる + - 必要性が高い場面 → ユーザー登録フローで不正アドレスを弾きたい、スパム対策など + - 正規表現だけでは不十分な場面 → 実際に送信して確認リンクを踏ませる二段階確認が必要 \ No newline at end of file From 1db4c15179812f916cf07bc60b494ce1621a213e Mon Sep 17 00:00:00 2001 From: busker <165013324+hiroki-horiguchi-dev@users.noreply.github.com> Date: Tue, 5 May 2026 14:13:24 +0900 Subject: [PATCH 4/4] Update 929.md --- hashmap/929.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hashmap/929.md b/hashmap/929.md index 669316d..2131fbd 100644 --- a/hashmap/929.md +++ b/hashmap/929.md @@ -83,7 +83,7 @@ class Solution { - https://github.com/akmhmgc/arai60/pull/11#discussion_r2311995496 1. RFC 5322を知っているか? -> メールアドレスには公式仕様があり、ローカルパートに ' や " が使えるなど、想像以上に複雑だという認識があるか。 2. 正規表現を知っているか - -> 単純な文字列操作だけでなく、パターンマッチの道具として正規表現を持っているか。 + -> 単純な文字列操作だけでなく、パターンマッチの道具として正規表現を持っているか。[参考](https://www.regular-expressions.info/email.html) 3. 正規表現での完全な検証が困難だと知っているか ->「99%カバーできるが完璧はない」というトレードオフの話ができるか。 4. フォローアップ:バリデーションが本当に必要な場面は?