Skip to content

Create Unique_email_address.md#7

Open
nittoco wants to merge 1 commit intomainfrom
nittoco-patch-7
Open

Create Unique_email_address.md#7
nittoco wants to merge 1 commit intomainfrom
nittoco-patch-7

Conversation

@nittoco
Copy link
Copy Markdown
Owner

@nittoco nittoco commented Apr 28, 2024

問題文URL: https://leetcode.com/problems/unique-email-addresses/description/
Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'.

For example, in "alice@leetcode.com", "alice" is the local name, and "leetcode.com" is the domain name.
If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.

For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.
If you add a plus '+' in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names.

For example, "m.y+name@email.com" will be forwarded to "my@email.com".
It is possible to use both of these rules at the same time.

Given an array of strings emails where we send one email to each emails[i], return the number of different addresses that actually receive mails.

Example 1:

Input: emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails.
Example 2:

Input: emails = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"]
Output: 3


- splitメソッドを知らなかった
- $が終わりマークなので、「〇〇以降のものを取り除く」という処理ができる
- 変数名が、中身が変化していってもずっと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.

特に忌避感は感じませんでした。


class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
email_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.

email_set の _set は、変数の型に含まれているため、情報が重複しています。外したほうが良いと思います。 unique_emails はいかがでしょうか?

local_name, domain_name = email.split('@', 1)
local_name = re.sub('\.', '', local_name)
local_name = re.sub('\+.*$', '', local_name)
email_set.add(str.join(local_name, domain_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.

['ab@c', 'a@bc'] のときに正しい解を返さないように思いました。

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.

これ気づいてませんでした。ありがとうございます


email_set = set()
for email in emails:
print(canonicalize(email))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

print すると、実行時に標準出力を汚してしまいます。意図して行う場合以外は、提出する前、またはレビューする前に削除しましょう。

def canonicalize(email):
if len(email.split('@')) != 2:
raise Exception('Invalid Inputs')
local_name = email.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.

split 2回はちょっと違和感ですね。上みたいに
local_name, domain_name = email.split('@', 1)
のほうがいいでしょう。

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