Skip to content

Create UniqueEmailAddresses.md#11

Open
kt-from-j wants to merge 1 commit intomainfrom
UniqueEmailAddresses
Open

Create UniqueEmailAddresses.md#11
kt-from-j wants to merge 1 commit intomainfrom
UniqueEmailAddresses

Conversation

@kt-from-j
Copy link
Owner

今回解いた問題:
929. Unique Email Addresses
次に解く問題:
387. First Unique Character in a String

Copy link

@nanae772 nanae772 left a comment

Choose a reason for hiding this comment

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

全体的に読みやすかったです。

  • RFCにメールアドレスの仕様があること
  • 実際にRFCの仕様通りかをvalidateするのは大変であること

などがdiscordで話されていたと思うのでその辺りも見ておくとよいかもしれません。

Comment on lines +31 to +34
if (!email.contains("@") || email.indexOf("@") != email.lastIndexOf("@")) {
// あるいはExceptionを投げる?
return null;
}
Copy link

Choose a reason for hiding this comment

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

このあたり「@が0個または2個以上」、つまり@がちょうど1個だということを表しているのだと思いますがemailの@の個数を直接数えられればいいのかなと思いました。
ただ、Javaだとそんなに簡潔にやる方法が無いっぽいですね…一応GPTに聞いてみた感じだと以下のような方法が比較的分かりやすいかなと感じました。

    long count_at = email.chars().filter(c -> c == '@').count();
    if (count_at != 1) {
      // あるいはExceptionを投げる?
      return null;
    }

javaの文字コード周りがあまり分かっておらずASCII以外の文字が来た時に上手くいくかはちょっと分かってないのですがご参考になれば幸いです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ご提案いただいたコードの方が'@'が1つしか含まれていないことをチェックしていると分かりやすいですね!

javaの文字コード周りがあまり分かっておらずASCII以外の文字が来た時に上手くいくかはちょっと分かってない

絵文字とかサロゲートペアを利用する文字以外なら、この実装で大丈夫です。
https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Character.html

Javaプラットフォームでは、char配列、Stringクラス、およびStringBufferクラスでUTF-16表現が使用されます。

Comment on lines +141 to +161
```java
class Solution {
public int numUniqueEmails(String[] emails) {
Set<String> uniqueEmails = new HashSet<>();
for (String email : emails) {
uniqueEmails.add(canonicalizeEmail(email));
}
return uniqueEmails.size();
}

private String canonicalizeEmail(String email) {
String[] localNameAndDomainName = email.split("@");
String localName = localNameAndDomainName[0];
String domainName = localNameAndDomainName[1];

localName = localName.split("\\+")[0];
localName = localName.replaceAll("\\.", "");
return localName + "@" + domainName;
}
}
```
Copy link

Choose a reason for hiding this comment

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

読みやすいです


private String canonicalizeEmail(String email) {
// 「ローカル名」「ローカル名の+以下の部分」「ドメイン名」の3領域をマッチ
Pattern emailPattern = Pattern.compile("^([a-z\\.]+)(\\+[a-z\\.]*)?@([a-z\\.\\+]+)$");
Copy link

Choose a reason for hiding this comment

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

この正規表現は、読むのにやや認知負荷がかかるように思いました。 step3 のように、シンプルな Java のコードで書いたほうが、読み手にとって読みやすいかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

私もそのように思います。
ある程度複雑な正規表現を扱う際はコメントなどで補足するように心がけたいと思います。

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.

4 participants