831. Masking Personal Information

简单题,没啥技巧

class Solution {
public:
        
    // 个人邮件和电话二选一
    string maskPII(string S) {
        if (S.size()==0){return "";}
        
        for(int i=0;i<S.size();i++){
            if (S[i]>='A'&&S[i]<='Z'){
                S[i] = S[i]-'A'+'a';
            }
        }
        
        // "LeetCode@LeetCode.com" name@name.name name中只有大写或小写字母
        // => lower(name[0]*****name[-1]@name.name)
        // must add five ***** for email
        if (S[0]>='a'&&S[0]<='z'){// email
            int index1 = S.find("@"); // [0, index1-1]
            int index2 = S.find("."); // [index1+1, index2-1] // [index2+1, end]
            string name1 = S.substr(0, index1-1-0+1);
            string name2 = S.substr(index1+1, index2-1-index1-1+1);
            string name3 = S.substr(index2+1, S.size()-1-index2-1+1);
            string res = "";
            res = res+name1[0]+"*****"+name1[name1.size()-1]+"@"+name2+"."+name3;
            return res;
        }
            
    
        // phone num // loca number
        // 1(234)567-890 // last 10 digits=>local number=>***-***-7890
                         // others => country code (has or not) if has
                         // +len(country code)*'*'-
                         // 删去 '('  ')' ' '
        else{// digits
            string digits="";
            for (int i=0;i<S.size();i++){
                if (S[i]>='0'&&S[i]<='9'){
                    digits+=S[i];
                }
            }
            
            if (digits.size()==10){
                string res = "";
                res = res + "***-***-"+digits[digits.size()-4]+digits[digits.size()-3]+digits[digits.size()-2]+digits[digits.size()-1];
                return res;
            }
            else{
                string res = "+";
                for(int i=0;i<digits.size()-10;i++){
                    res += "*";
                }
                res = res + "-***-***-"+digits[digits.size()-4]+digits[digits.size()-3]+digits[digits.size()-2]+digits[digits.size()-1];   
                return res;
            }
            
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值