Skip to content

Create Unique Email Addresses.md#4

Open
aiueoriku wants to merge 3 commits intomainfrom
Unique-Email-Addresses
Open

Create Unique Email Addresses.md#4
aiueoriku wants to merge 3 commits intomainfrom
Unique-Email-Addresses

Conversation

@aiueoriku
Copy link
Copy Markdown
Owner

https://github.com/potrue/leetcode/pull/14/files#diff-6a8efe56493bdbfeb045b7aa123660406ebdd5dd4f0a011935562d4eefb9ab46

上記を見ると,概ね実装方針は同じだった.ただ,以下が参考になった.
- listの代わりにset()を使っていたこと.setは重複を排除する.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

要素数 n の list では in は O(n) かかります。set の場合は O(1) という違いもあります。
https://wiki.python.org/moin/TimeComplexity


上記を見ると,概ね実装方針は同じだった.ただ,以下が参考になった.
- listの代わりにset()を使っていたこと.setは重複を排除する.
- @が含まれない場合も考慮して,partitionを使用していたこと.partitionは区切り文字も返す.
Copy link
Copy Markdown

@tokuhirat tokuhirat May 23, 2025

Choose a reason for hiding this comment

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

Step3では、@ が含まれない場合、全体を local として受け入れて処理する方針になっていますね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

そういう意味だったのですね.誤った認識をしてました.ご指摘ありがとうございます.

local, domain = email.split("@")
local = local.replace(".", "")
local = local.split("+")[0]
email = local+"@"+domain
Copy link
Copy Markdown

@huyfififi huyfififi May 23, 2025

Choose a reason for hiding this comment

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

私はf-stringの方が結果の文字列が想像しやすくて好きです。Googleのスタイルガイドにも、

Use an f-string...(中略)...A single join with + is okay but do not format with +.
No: x = first + ', ' + second

とあり、私個人は、スタイルガイドにあるということは多くの人がf-stringの方を好ましく思うのだろう、と想定しています。

for email in emails:
local, at, domain = email.partition("@")
local = local.replace(".", "")
local = local.split("+")[0]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

先に無視される部分を決めた方が(+の処理を先に行う)効率的ではないでしょうか。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

仰るとおりですね.ありがとうございます.

local, domain = email.split("@")
local = local.replace(".", "")
local = local.split("+")[0]
email = local+"@"+domain
Copy link
Copy Markdown

@tokuhirat tokuhirat May 23, 2025

Choose a reason for hiding this comment

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

書き方として email = f"{local}@{domain}" というパターンもありますね。
https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals

- listの代わりにset()を使っていたこと.setは重複を排除する.
- @が含まれない場合も考慮して,partitionを使用していたこと.partitionは区切り文字も返す.

また,RFC規格という存在を知った.https://info.yamap.com/archives/3434
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私も間違った認識をしていましたが、 RFCは規格ではないようです.

rinost081/LeetCode#13 (comment)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

規格ではなく,文書ということですね.

- @が含まれない場合も考慮して,partitionを使用していたこと.partitionは区切り文字も返す.

また,RFC規格という存在を知った.https://info.yamap.com/archives/3434

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

初めて正規表現の概念を知りました.正規表現を使って解き直してみます.ありがとうございます.

他の人の解答を参照する.
https://github.com/potrue/leetcode/pull/14/files#diff-6a8efe56493bdbfeb045b7aa123660406ebdd5dd4f0a011935562d4eefb9ab46

上記を見ると,概ね実装方針は同じだった.ただ,以下が参考になった.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1文字ずつ処理する方法も選択肢として持って良いと思います!

local, at, domain = email.partition("@")
local = local.replace(".", "")
local = local.split("+")[0]
email = local + at + domain
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

partitionするときにlist型で取っておいてjoinすることもできそうですね

def numUniqueEmails(self, emails: List[str]) -> int:
valid_emails = set()
for email in emails:
local, domain = email.split('@')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

re.matchやre.fullmatchあたりを使うこともできると思います
https://docs.python.org/ja/3.13/library/re.html#re.match

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.

7 participants