Skip to content

Latest commit

 

History

History
51 lines (41 loc) · 3.22 KB

File metadata and controls

51 lines (41 loc) · 3.22 KB

⚽️ 929. Unique Email Addresses

  • Date : 2022.02.27(월)
  • Time : 30분

Problem

  • 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.
  • 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.
  • 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.

Constraints

  • 1 <= emails.length <= 100
  • 1 <= emails[i].length <= 100
  • emails[i] consist of lowercase English letters, '+', '.' and '@'.
  • Each emails[i] contains exactly one '@' character.
  • All local and domain names are non-empty.
  • Local names do not start with a '+' character.
  • Domain names end with the ".com" suffix.

Example

풀이

    def numUniqueEmails(self, emails: List[str]) -> int:
        uniq_email_list = set()
        for email in emails:
            local_name, domain_name = email.split('@')

            if local_name.find('+') > 0:
                local_name = local_name.split('+')[0]
            if local_name.find('.') > 0:

                local_name_arr = local_name.split('.')
                local_name = ''.join(local_name_arr)

            entry = local_name + '@' + domain_name
            uniq_email_list.add(entry)
        return len(uniq_email_list)

: 이 문제는 특정 조건을 통해 유니크한 이메일의 갯수를 찾아내는 문제이다. 먼저 이메일에서 특별한 기호가 3개 등장한다. 바로 @, +, . 이다. 먼저 메일에서 @ 기호를 통해 우리는 앞부분을 로컬, 뒷부분을 도메인이라고 명할 수 있다. 그래서 먼저 "@"를 통해서 local_name과 domain_name으로 나눠준 것이다. 이제 로컬 네임에서 두개의 기호를 판단해야한다. 먼저 + 기호는 더하기 기호의 뒷 부분이 모두 삭제된다. 그래서 기호의 앞부분인 [0]만 살려서 local_name으로 재정의해준다. 그리고 두번째로 . 기호는 그냥 점 기호를 무시해주면 된다(ㅎㅎ). 그리고 이제 이 두개를 다시 주소로 만든다. 여기서 uniq_email_list를 set으로 저장해준 이유는 이렇게 모든 판정을 거친 후에 메일이 겹칠 수 있기 때문이다.