見出し画像

ほぼ日刊競プロ leetcode 929. Unique Email Addresses

929. Unique Email Addresses


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.

考えたこと

@を起点に前の文字列に処理を行い,最後に結合させる

1.@の位置を探す
2.@より前の文字列に対して「.」を削除する
3.上記の文字列に対して「+」以下を無視するように正規表現を使い,削除する(re をimportしている)
4.@以下の文字列と上記の文字列を結合させる
5.set関数を使い,重複を削除する

import re
class Solution:
   def numUniqueEmails(self, emails: List[str]) -> int:
       print (emails)
       ans = []
       for mail in emails:
           temp = mail.find("@")
           temp1 = mail[0:temp].replace(".","")
           temp2 = re.sub('\+[a-z]+', '', temp1)
           temp3 = mail[temp::]
           ans.append(temp2+temp3)
       return len(set(ans))

いいなと思ったら応援しよう!