Unique Email Addresses

本文探讨了如何处理包含特殊字符的电子邮件地址,通过去除点号和忽略加号后的文本,识别并计算实际接收邮件的不同地址数量。使用Golang实现,对比了两种算法效率,最终采用map数据结构优化解决方案。

Unique Email Addresses

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

For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.

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

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. For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address. (Note that this rule does not apply for 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, for example m.y+name@email.com will be forwarded to my@email.com. (Again, this rule does not apply for domain names.)

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?

Example 1:

Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails

Note:

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

问题剖析

上述问题本质上是对字符串的处理问题,需要用到字符串的分割,合并,替换等操作。难度本身不大,只是为了熟悉字符串操作而已。所以本人首先了一个效率不怎么高的解法,拆分字符串数组,对每个"邮箱地址"进行处理。代码如下所示:

func numUniqueEmails(emails []string) int {
    
    if emails == nil {
        return 0
    }
    
    actual := make([]string, 0)
    
    for _, email := range emails {
        
        local := strings.Split(email, "@")[0]
        domain := strings.Split(email, "@")[1]
        
        if strings.Index(local, "+") != -1 {
            local = strings.Split(local, "+")[0]
        }
        
        local = strings.Replace(local, ".", "", -1)
        
        newEmail := fmt.Sprintf("%s@%s", local, domain)
        
        if !isInActual(actual, newEmail) {
            actual = append(actual, newEmail)
        }
    }
    
    return len(actual)
}

func isInActual(actual []string, str string) bool {
    isIn := false
    
    for _, item := range actual {
        if item == str {
            isIn = true
        }
    }
    return isIn
}

这个解法虽然搞定了需求,但是有很多多余的操作,比如判断是否是重复的字符串的时候,其实没必要通过函数调用isInActual,去一次次的遍历整个列表。这样的操作是O(n^2)时间复杂度。而golang本身提供了map这样的数据结构可以去重,所以代码可以再次简化:

func numUniqueEmails(emails []string) int {
    
    if emails == nil {
        return 0
    }
    
    e_map := make(map[string]int)
    
    for idx, email := range emails {
        
        local  := strings.Split(email, "@")[0]
        domain := strings.Split(email, "@")[1]
        if strings.Index(local, "+") != -1 {
            local = strings.Split(local, "+")[0]
        }
        
        local = strings.Replace(local, ".", "", -1)
        
        newEmail := fmt.Sprintf("%s@%s", local, domain)
        
        if _, ok:= e_map[newEmail]; !ok {
            e_map[newEmail] = idx
        }
    }

    return len(e_map)
}

整个解法比上个解法整整快了一倍,时间比较如下:
在这里插入图片描述
欢迎大家进行改进,希望得到更快的解法。

leetcode上有个哥们用了比较复杂的表达式进行切分,也得到了结果。代码比较简短。但是可读性不太好,而且比我的第二个解法还慢了一些。代码示例如下:

func numUniqueEmails(emails []string) int {
	m := make(map[string]int)
	for i := 0; i < len(emails); i++ {
		parsedEmail := strings.Join(strings.Split(emails[i][0:strings.Index(emails[i], "+")-1], "."), "") + emails[i][strings.Index(emails[i], "@"):]
		if _, ok := m[parsedEmail]; ok {
			continue
		}
		m[parsedEmail] = i
	}
	result := len(m)
	return result
}
-- 用户表 CREATE TABLE users ( user_id BIGINT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(100) NOT NULL COMMENT 'BCrypt加密', phone VARCHAR(20) NOT NULL, register_time DATETIME DEFAULT CURRENT_TIMESTAMP, role_id INT NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 商品分类表 CREATE TABLE categories ( category_id INT PRIMARY KEY AUTO_INCREMENT, category_name VARCHAR(50) NOT NULL UNIQUE, parent_id INT DEFAULT NULL, create_time DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (parent_id) REFERENCES categories(category_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 用户表 CREATE TABLE users ( user_id BIGINT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(100) NOT NULL COMMENT 'BCrypt加密', phone VARCHAR(20) NOT NULL, register_time DATETIME DEFAULT CURRENT_TIMESTAMP, role_id INT NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 商品分类表 CREATE TABLE categories ( category_id INT PRIMARY KEY AUTO_INCREMENT, category_name VARCHAR(50) NOT NULL UNIQUE, parent_id INT DEFAULT NULL, create_time DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (parent_id) REFERENCES categories(category_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 用户表 CREATE TABLE users ( user_id BIGINT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(100) NOT NULL COMMENT 'BCrypt加密', phone VARCHAR(20) NOT NULL, register_time DATETIME DEFAULT CURRENT_TIMESTAMP, role_id INT NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 商品分类表(新增) CREATE TABLE categories ( category_id INT PRIMARY KEY AUTO_INCREMENT, category_name VARCHAR(50) NOT NULL UNIQUE, parent_id INT DEFAULT NULL, create_time DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (parent_id) REFERENCES categories(category_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 用户表 CREATE TABLE users ( user_id BIGINT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(100) NOT NULL COMMENT 'BCrypt加密', phone VARCHAR(20) NOT NULL, register_time DATETIME DEFAULT CURRENT_TIMESTAMP, role_id INT NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 商品分类表 CREATE TABLE categories ( category_id INT PRIMARY KEY AUTO_INCREMENT, category_name VARCHAR(50) NOT NULL UNIQUE, parent_id INT DEFAULT NULL, create_time DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (parent_id) REFERENCES categories(category_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 用户表(需最先创建,因为其他表有外键依赖) CREATE TABLE users ( user_id BIGINT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(100) NOT NULL COMMENT 'BCrypt加密', phone VARCHAR(20) NOT NULL UNIQUE, -- 添加 UNIQUE 约束(如果手机号需唯一) email VARCHAR(100) UNIQUE, -- 添加 UNIQUE 约束(如果邮箱需唯一) register_time DATETIME DEFAULT CURRENT_TIMESTAMP, INDEX idx_username (username), INDEX idx_phone (phone), -- 为手机号添加索引 INDEX idx_email (email) -- 为邮箱添加索引 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 订单表 CREATE TABLE orders ( order_id BIGINT PRIMARY KEY AUTO_INCREMENT, order_no VARCHAR(32) NOT NULL UNIQUE COMMENT '订单编号', total_amount DECIMAL(12,2) NOT NULL, status ENUM('PENDING', 'PAID', 'SHIPPED', 'COMPLETED', 'CANCELLED', 'REFUNDED') DEFAULT 'PENDING', buyer_id BIGINT NOT NULL, address_id BIGINT NOT NULL, payment_time DATETIME DEFAULT NULL, create_time DATETIME DEFAULT CURRENT_TIMESTAMP, update_time DATETIME ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (buyer_id) REFERENCES users(user_id), FOREIGN KEY (address_id) REFERENCES addresses(address_id), INDEX idx_buyer (buyer_id), INDEX idx_create_time (create_time), CONSTRAINT chk_amount CHECK (total_amount > 0) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 订单项表 CREATE TABLE order_items ( item_id BIGINT PRIMARY KEY AUTO_INCREMENT, order_id BIGINT NOT NULL, product_id BIGINT NOT NULL, product_name VARCHAR(100) NOT NULL COMMENT '下单时的商品快照', quantity INT NOT NULL, unit_price DECIMAL(10,2) NOT NULL, total_price DECIMAL(10,2) GENERATED ALWAYS AS (quantity * unit_price) VIRTUAL, -- 修正类型并改为 VIRTUAL FOREIGN KEY (order_id) REFERENCES orders(order_id) ON DELETE CASCADE, FOREIGN KEY (product_id) REFERENCES products(product_id), CONSTRAINT chk_quantity CHECK (quantity > 0), CONSTRAINT uk_order_product UNIQUE (order_id, product_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;生成er图
05-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值