-
Notifications
You must be signed in to change notification settings - Fork 0
Hashmap 929 #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Hashmap 929 #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| - 問題: [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` | ||
| - `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 に対して2つの処理を実行 | ||
| - local name と domain name を連結する | ||
| - Set に詰める | ||
| - Set のサイズを返却する | ||
| - 時間 | ||
| - 方針を考えるところまで3分 | ||
| - 時間計算量: `O(N)` | ||
| - 空間計算量: `O(N)` | ||
| - 初期方針 | ||
| ```java | ||
| class Solution { | ||
| public int numUniqueEmails(String[] emails) { | ||
| Set<String> 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 を使ったので少し冗長なコードになった | ||
| - 初期方針で立てていた、「remove dots」「remove after + symbol」の順序は逆の方が計算回数が少なくて済む | ||
| ```java | ||
| class Solution { | ||
| public int numUniqueEmails(String[] emails) { | ||
| Set<String> uniqueEmails = new HashSet<>(); | ||
| for (String email : emails) { | ||
| // split at @ index. | ||
| String[] pair = email.split("@"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます、おっしゃる通り少し悩みました。 |
||
| 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(); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## いただいたコメントから要点を整理 | ||
| - https://github.com/akmhmgc/arai60/pull/11#discussion_r2311995496 | ||
| 1. RFC 5322を知っているか? -> メールアドレスには公式仕様があり、ローカルパートに ' や " が使えるなど、想像以上に複雑だという認識があるか。 | ||
| 2. 正規表現を知っているか | ||
| -> 単純な文字列操作だけでなく、パターンマッチの道具として正規表現を持っているか。[参考](https://www.regular-expressions.info/email.html) | ||
| 3. 正規表現での完全な検証が困難だと知っているか | ||
| ->「99%カバーできるが完璧はない」というトレードオフの話ができるか。 | ||
| 4. フォローアップ:バリデーションが本当に必要な場面は? | ||
| - 必要性が低い場面 → 社内ツールで入力者が信頼できる | ||
| - 必要性が高い場面 → ユーザー登録フローで不正アドレスを弾きたい、スパム対策など | ||
| - 正規表現だけでは不十分な場面 → 実際に送信して確認リンクを踏ませる二段階確認が必要 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
akmhmgc/arai60#11 (comment)
もしご覧になっていなければ、このあたりも見てもいいと思います。
こういった記事もあります。
https://www.regular-expressions.info/email.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確認できてなかったです、ありがとうございます!