Masking Personal Information

We are given a personal information string S, which may represent either an email address or a phone number.

We would like to mask this personal information according to the following rules:

1. Email address:

We define a name to be a string of length ≥ 2 consisting of only lowercase letters a-z or uppercase letters A-Z.

An email address starts with a name, followed by the symbol '@', followed by a name, followed by the dot '.' and followed by a name.

All email addresses are guaranteed to be valid and in the format of "name1@name2.name3".

To mask an email, all names must be converted to lowercase and all letters between the first and last letter of the first name must be replaced by 5 asterisks '*'.

2. Phone number:

A phone number is a string consisting of only the digits 0-9 or the characters from the set {'+', '-', '(', ')', ' '}. You may assume a phone number contains 10 to 13 digits.

The last 10 digits make up the local number, while the digits before those make up the country code. Note that the country code is optional. We want to expose only the last 4 digits and mask all other digits.

The local number should be formatted and masked as "***-***-1111", where 1 represents the exposed digits.

To mask a phone number with country code like "+111 111 111 1111", we write it in the form "+***-***-***-1111".  The '+' sign and the first '-' sign before the local number should only exist if there is a country code.  For example, a 12 digit phone number mask should start with "+**-".

Note that extraneous characters like "(", ")", " ", as well as extra dashes or plus signs not part of the above formatting scheme should be removed.

Return the correct "mask" of the information provided.

Example 1:

Input: "LeetCode@LeetCode.com"
Output: "l*****e@leetcode.com"
Explanation: All names are converted to lowercase, and the letters between the
  first and last letter of the first name is replaced by 5 asterisks.
  Therefore, "leetcode" -> "l*****e".

Example 2:

Input: "AB@qq.com"
Output: "a*****b@qq.com"
Explanation: There must be 5 asterisks between the first and last letter
  of the first name "ab". Therefore, "ab" -> "a*****b".

Example 3:

Input: "1(234)567-890"
Output: "***-***-7890"
Explanation: 10 digits in the phone number, which means all digits make up the local number.

Example 4:

Input: "86-(10)12345678"
Output: "+**-***-***-5678"
Explanation: 12 digits, 2 digits for country code and 10 digits for local number.

Notes:

  1. Emails have length at least 8.
  2. Phone numbers have length at least 10.
  3. S.length <= 40.
 1     string maskPII(string S) {
2 string res;
3 string test;
4 if(('a'<=S[0]&&S[0]<='z')||('A'<=S[0]&&S[0]<='Z')){ //别连写了
5 if('A'<=S[0]&&S[0]<='Z'){
6 res += S[0]+32; //string+char的用法
7 }else{
8 res += S[0];
9 }
10 res += "*****";
11 int i = 0;
12 for(i ; i < S.length();i++){
13 if(S[i] == '@'){
14 //test += i + '0';
15 break;
16 }
17 }
18 //test += S[i-1];
19 if('A'<=S[i-1]&&S[i-1]<='Z'){
20 res += S[i-1]+32;
21 }else{
22 res += S[i-1];
23 }
24 for(i;i<S.length();i++){
25 if('A'<=S[i]&&S[i]<='Z'){
26 res += S[i]+32;
27 }else{
28 res += S[i];
29 }
30 }
31 }else{
32 vector<int> data;
33 for(int i = 0; i < S.length();i++){
34 if('0'<=S[i]&&S[i]<= '9'){ //isdigit()函数可用
35 data.push_back(S[i] - '0');
36 }
37 }
38 //test += '0'+data.size();
39 if(data.size() > 10){
40 res += '+';
41 for(int j = 0; j < data.size() - 10;j++){
42 res += '*';
43 }
44 res += '-';
45 }
46 res += "***-***-";
47 for(int i = data.size() - 4; i < data.size(); i++){
48 res += data[i] + '0';
49 }
50 }
51 return res;
52 }

注意点:

  • isdigit()
  • string + char可直接加

Masking Personal Information的更多相关文章

  1. 【LeetCode】831. Masking Personal Information 解题报告(Python)

    [LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  2. [LeetCode] Masking Personal Information 给个人信息打码

    We are given a personal information string S, which may represent either an email address or a phone ...

  3. [Swift]LeetCode831. 隐藏个人信息 | Masking Personal Information

    We are given a personal information string S, which may represent either an email address or a phone ...

  4. English trip V1 - B 15. Giving Personal Information 提供个人信息 Teacher:Solo Key: Do/Does

    In this lesson you will learn to answer simple questions about yourself.  本节课讲学到回答关于自己的一些简单问题 课上内容(L ...

  5. English trip -- Review Unit1 Personal Information 个人信息

    1.重点内容进行自我介绍 What's you name? I'm Loki Where are you from? I'm Local, I'm Chengdu How old are you? t ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  8. 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

    Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...

  9. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

随机推荐

  1. LVM的简单使用及常用的命令总结

    Lvm的简单使用及常用的命令总结 centos7中默认使用的是xfs文件系统,此文件系统的特点,可以另外查找资料,这里说一下对文件系统的扩容: 1.先看一下没扩容之前的分区大小 2.添加一块新磁盘,并 ...

  2. 045 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 07 for循环应用及局部变量作用范围

    045 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 07 for循环应用及局部变量作用范围 本文知识点:for循环应用及局部变量作用范围 for循环 w ...

  3. python图像的绘制

    转载:https://blog.csdn.net/haoji007/article/details/52063168 实际上前面我们就已经用到了图像的绘制,如: io.imshow(img) 这一行代 ...

  4. MySQL中事务和事务的隔离级别

    本文主要是帮助理解相关知识,没有具体的操作和代码. 事务 事务就是一组操作,这组操作要么全部成功,要么全部失败. 最经典的例子就是银行转账: 张三给李四转账100,对用户来说,就是一个操作.但对应到数 ...

  5. JWT安全性第1部分,创建令牌

    下载Demo Core 2.0 - 13.2 MB 下载Demo Core 1.2 - 14 MB 介绍 JWT (JSON Web Token)作为保护Web站点和REST服务的标准越来越流行.我将 ...

  6. Eating Peach (peach)

    Description On this day, the little monkey went looking for food. He came to a rectangular peach gar ...

  7. IOS使用UITextView进行富文本编辑|纯干货

    看了好多blog介绍富文本编辑,有很多很好的开源项目,比如:YYText.FastTextView.ZSSRichTextEditor等等.本着学习的目的还是选择用UITextView来实现简单的富文 ...

  8. 使用docker搭建redis服务器记录

    #mkdir /home/redishome#mkdir /home/redishome/data#chmod -R 777 /home/redishome把redis.conf传到/home/red ...

  9. TMS, XYZ & WMTS的不同

    WMS是OGC定义的协议,用于请求任意区域的渲染地图图像.客户可以根据需要以平铺模式对其进行请求. WMS-C是OSGeo创建的WMS扩展,它向功能文档中添加了元数据,以使客户端知道在哪里发出请求,从 ...

  10. 多测师讲解接口测试 _HTTP常见的状态码归纳_高级讲师肖sir

    100 Continue  初始的请求已经接受,客户应当继续发送请求的其余部分 101 Switching Protocols  服务器将遵从客户的请求转换到另外一种协议 200 OK  一切正常,对 ...