|
| 1 | +> Note: |
| 2 | +> 自身で思いついた解法は、1.標準ライブラリを利用した解法 2.indexによる文字列チェックの関数化 3.正規表現による解法 |
| 3 | +> 問題文による制限がかなり強いため、楽になっていると感じた。ぱっとおもいつくのは、各コードに@の出現回数が1回の場合の例外処理を書く。 |
| 4 | +```python |
| 5 | +if len(email).split != 2: |
| 6 | + raise Exception('Invalid Email Input') |
| 7 | +``` |
| 8 | + |
| 9 | +## 標準ライブラリを利用した解法 |
| 10 | +### 1回目 (5m34s) |
| 11 | +時間計算量: O(N*M)<br> |
| 12 | +空間計算量: O(N)<br> |
| 13 | +N: len(emails), M: len(local_name) |
| 14 | + |
| 15 | +```python |
| 16 | +class Solution: |
| 17 | + def numUniqueEmails(self, emails: List[str]) -> int: |
| 18 | + unique_emails = set() |
| 19 | + |
| 20 | + for email in emails: |
| 21 | + local_name = email.split("@")[0].replace(".", "").split("+")[0] |
| 22 | + domain_name = email.split("@")[1] |
| 23 | + unique_emails.add(local_name + "@" + domain_name) |
| 24 | + return len(unique_emails) |
| 25 | +``` |
| 26 | + |
| 27 | + |
| 28 | +### 2回目 |
| 29 | +時間計算量: 1回目と同じ<br> |
| 30 | +空間計算量: 1回目と同じ<br> |
| 31 | + |
| 32 | +> 問題の制限から、emailは**1つの**@まで区切られているので、 |
| 33 | +> ```pyhton local_name, domain_name = email.split("@")```と1行に変更。 |
| 34 | +> 上記に合わせて、1行での読み取りの負荷を減らすために、replaced_local_nameを中間変数として作成。 |
| 35 | +> unique_emailsは、文字列の結合を行わずとも、タプルとして入れても良いかと思った。 |
| 36 | +> ちょっと変数名が長い気がした。_nameを削除。 |
| 37 | +
|
| 38 | + |
| 39 | +```python |
| 40 | +class Solution: |
| 41 | + def numUniqueEmails(self, emails: List[str]) -> int: |
| 42 | + unique_emails = set() |
| 43 | + |
| 44 | + for email in emails: |
| 45 | + local, domain = email.split("@") |
| 46 | + formatted_local = local.replace(".", "").split("+")[0] |
| 47 | + unique_emails.add(formatted_local + domain) |
| 48 | + return len(unique_emails) |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +## indexによる文字列チェックの関数化 |
| 53 | +時間計算量: O(N*M)<br> |
| 54 | +空間計算量: O(N)<br> |
| 55 | +N: len(emails), M: len(local_name) |
| 56 | + |
| 57 | +> ahayashiさんのコードの様に、localとdomainを分けず、'@'を処理に加えても良いも。 |
| 58 | +
|
| 59 | +```python |
| 60 | +class Solution: |
| 61 | + def numUniqueEmails(self, emails: List[str]) -> int: |
| 62 | + def format_local_address(oroginal_local: str) -> str: |
| 63 | + i = 0 |
| 64 | + replaced_local = "" |
| 65 | + while i < len(oroginal_local) and oroginal_local[i] != '+': |
| 66 | + if oroginal_local[i] != '.': |
| 67 | + replaced_local += oroginal_local[i] |
| 68 | + i += 1 |
| 69 | + return replaced_local |
| 70 | + |
| 71 | + unique_emails = set() |
| 72 | + |
| 73 | + for email in emails: |
| 74 | + local, domain = email.split("@") |
| 75 | + formatted_local = format_local_address(local) |
| 76 | + unique_emails.add(formatted_local + "@" + domain) |
| 77 | + |
| 78 | + return len(unique_emails) |
| 79 | +``` |
| 80 | + |
| 81 | +## 正規表現による解法 |
| 82 | +時間計算量: O(N*M)<br> |
| 83 | +空間計算量: O(N)<br> |
| 84 | +N: len(emails), M: len(local_name) |
| 85 | + |
| 86 | +```python |
| 87 | +class Solution: |
| 88 | + def numUniqueEmails(self, emails: List[str]) -> int: |
| 89 | + unique_emails = set() |
| 90 | + |
| 91 | + for email in emails: |
| 92 | + local, domain = email.split("@") |
| 93 | + formatted_local = re.sub(r'\.+', '', local) |
| 94 | + formatted_local = re.sub(r'\+.*', '', formatted_local) |
| 95 | + unique_emails.add(formatted_local + domain) |
| 96 | + |
| 97 | + return len(unique_emails) |
| 98 | +``` |
0 commit comments