929. Unique Email Addresses [Easy]

这篇博客探讨了在Java中如何使用String的split(), replaceAll()等方法以及正则表达式处理邮箱地址,特别是如何处理'.'和'+'这两个特殊字符。通过三个不同的解决方案,展示了如何避免特殊字符带来的问题,以及这些方法对运行时间和内存使用的影响。
摘要由CSDN通过智能技术生成

可以锻炼使用java String的split()、replacAll()等方法和正则表达式的题

思路很简单,但实现时有坑:

用'.'字符分割字符串,不能用s.split("."),因为'.'是特殊字符,而split()是用正则表达式匹配参数

这里需要用"\\."或"[.]"作为参数

split()特殊字符的坑:

https://blog.csdn.net/chiling0814/article/details/100723898?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-9.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-9.control

/**
 * Runtime: 7 ms, faster than 87.20%
 * Memory Usage: 39.3 MB, less than 62.20%
 */
class Solution {
    
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet(); // use set to avoid duplicate processed emails
        for (String email : emails) {
            // devide each email to a local name and a domain name
            String[] names = email.split("@");
            // ignore the substring of the local name after '+'
            int idx = names[0].indexOf('+');
            if (idx >= 0) {
                names[0] = names[0].substring(0, idx);
            }
            String[] segs = names[0].split("\\."); // '.' is special character and cannot be directly used to split String, use "\\." or "[.]" instead
            StringBuilder sb = new StringBuilder();
            for (String seg : segs) {
                sb.append(seg);
            }
            // recombine the processed local name with the domain name
            sb.append('@');
            sb.append(names[1]);
            set.add(sb.toString());
        }
        return set.size();
    }
    
}
/**
 * 用replaceAll()方法,用空字符串替换所有".",来忽略local name里的"."
 * 用split()方法将local name分成"+"前后的两个子串,只取第一个,来忽略local name里"+"后面的部分
 * 注意"."和"+"都要写成正则表达式版
 * 简化了代码但用了没必要的方法,效率降低
 * Runtime: 25 ms, faster than 30.68%
 * Memory Usage: 40.4 MB, less than 27.01%
 */
class Solution {
    
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet(); // use set to avoid duplicate processed emails
        for (String email : emails) {
            String[] names = email.split("@");
            names[0] = names[0].replaceAll("\\.", ""); // ignore '.' in the local name
            StringBuilder sb = new StringBuilder();
            sb.append(names[0].split("\\+")[0]); // ignore the substring after '+' in the local name
            sb.append("@");
            sb.append(names[1]);
            set.add(sb.toString());
        }
        return set.size();
    }
    
}
/**
 * 相比于上一段,没有用StringBuilder,直接字符串相加
 * 代码越来越精简,但效率越来越低
 * Runtime: 36 ms, faster than 16.55%
 * Memory Usage: 40.5 MB, less than 25.59%
 */
class Solution {
    
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet(); // use set to avoid duplicate processed emails
        for (String email : emails) {
            String[] names = email.split("@");
            names[0] = names[0].replaceAll("\\.", ""); // ignore '.' in the local name
            set.add(names[0].split("\\+")[0] + "@" + names[1]);
        }
        return set.size();
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值