Skip to content

929. Unique Email Addresses#19

Open
Yuto729 wants to merge 1 commit intomainfrom
unique-email-addresses
Open

929. Unique Email Addresses#19
Yuto729 wants to merge 1 commit intomainfrom
unique-email-addresses

Conversation

@Yuto729
Copy link
Copy Markdown
Owner

@Yuto729 Yuto729 commented Dec 10, 2025

@Yuto729 Yuto729 changed the title Unique Email Addresses 929. Unique Email Addresses Dec 10, 2025
```py
class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
def parse_local_name(local_name):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数名から、どのような値が返ってくるのかが分かりにくく感じました。比較のために正規化する意味合いを込めて、 canonicalize() はいかがでしょうか?引数の名前が local_name なのであれば、関数名に重複して入れる必要はないと思います。

if local_name[0] == '+':
raise ValueError("local name start with plus operator.")

return local_name.split('+')[0].replace('.', '')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

圧縮され過ぎていて分かりにくく感じました。適宜分割したほうが良いと思います。

before_plus = local_name.split('+')[0]
return before_plus.replace('.', '')

return len(unique_emails)
```
`+`による文字列の結合はオブジェクトの再生成を伴うので効率が悪い. ユニークな数がわかればいいのでタプルをキーにすれば良さそう.
平均7 ms => 3 msほどに改善
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LeetCode 上での計測でしょうか? LeetCode の時間計測は誤差が大きいため、あまり信用しないほうが良いと思います。時間を計測するのであれば、ローカルで十分な回数実行し、平均値や中央値を取ったほうが良いと思います。
また、ミリ秒単位の実行時間がボトルネックにならないのであれば、実行時間ではなく、コードの可読性やメンテナンス性等、別の部分に注力したほうが良いと思います。さらに言えば、ミリ秒単位の実行時間を気にするシチュエーションなら、 C++ 等、 Python より高速な言語を使うことをおすすめします。

continue

if c == '+':
first_plus_appeared = True
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここで break したほうが、 first_plus_appeared も消せて、シンプルになると思います。

if not domain_name.endswith(".com"):
return

if domain_name[:-4] == "":
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-4がマジックナンバーになっていると感じました。(どこ由来かな?と考えました。)
".com"の長さであることが分かるようにするのが個人的に良いと思いました。

Suggested change
if domain_name[:-4] == "":
if domain_name == len(".com"):

Repository owner deleted a comment from github-actions bot Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants