搜索
您的当前位置:首页正文

LeetCode: 929. Unique Email Addr

来源:哗拓教育

Every email consists of a local name and a domain name, separated by the @ sign.

Besides lowercase letters, these emails may contain '.'s or '+'s.

It is possible to use both of these rules at the same time.

Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails?


理解一下:
一个 邮件地址的数组 emails = [String]()
筛选出其中不同的邮件地址. 有几种情况被判定为相同邮件地址.

如果 . 符号, 在 @ 符号后面则不忽略 . 符号

例子:

Input:
 
  
  
Output: 2

Note:

1 <= emails[i].length <= 100
1 <= emails.length <= 100
Each emails[i] contains exactly one '@' character.

我的写法

class Solution {
    func numUniqueEmails(_ emails: [String]) -> Int {
        var email_dict = [String: Bool]()
        for (_, email) in emails.enumerated() {
            var email_key = ""
            var can_offset = true
            var before_point = true
            for (offset, element) in email.enumerated() {
                if 0 == offset, element == "+" { break }
                if element == "+" { can_offset = false }
                if element == "@" {
                    can_offset = true
                    before_point = false
                }
                if "." != element, can_offset {
                    email_key.append(element)
                }
                
                if "." == element, false == before_point {
                    email_key.append(element)
                }
            }
            if !email_key.isEmpty {
                email_dict[email_key] = true
            }
        }
        return email_dict.count
    }
}
Top