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. jwtUtils顾名思意

    1 package com.pipihao.blog.util; 2 import java.util.Date; 3 4 import javax.crypto.SecretKey; 5 impor ...

  2. 《New Horizon College English》 (Third Edition) -长篇阅读(Skmming and Scanning)

    <New Horizon College English>(Third Edition) <新视野大学英语>(第三版) 长篇阅读(Skmming and Scanning) 总 ...

  3. linux系统上用户态pppoe收发包过程

    花了几天看了一下ppp/pppoe有关的东西,画了一下用户态pppoe收发包的示意图.

  4. 多测师讲解自动化测试 _RF分配id_高级讲师肖sir

    1.Assign Id To Element.

  5. 多测师_测试理轮_002(v模型和H模型)

    为什么要测试?1.软件非正常运行或自身缺陷会引发问题2.代码和文档是人写的,难免会出错3.环境原因影响软件(内存不足,存储,数据库溢出等)4.软件测试活动是保证软件质量的关键之一 什么是测试?软件行业 ...

  6. 如何给LG gram写一个Linux下的驱动?

    其实就是实现一下几个Fn键的功能,没有标题吹得那么牛. 不知道为啥,LG gram这本子意外的小众. 就因为这个,装Linux遇到的硬件问题就没法在网上直接搜到解决办法了. Fn + F9 实现阅读模 ...

  7. lambda函数小结

    C++中的lambda函数 lambda函数是函数式编程中的概念,由C++11引入,成为现代C++中重要的特性. 所谓lambda函数就是匿名函数,语法结构: [capture list] (para ...

  8. 【值得收藏】C语言入门基础知识大全!从C语言程序结构到删库跑路!

    01 C语言程序的结构认识 用一个简单的c程序例子,介绍c语言的基本构成.格式.以及良好的书写风格,使小伙伴对c语言有个初步认识. 例1:计算两个整数之和的c程序: #include main() { ...

  9. Tensorflow学习笔记No.7

    tf.data与自定义训练综合实例 使用tf.data自定义猫狗数据集,并使用自定义训练实现猫狗数据集的分类. 1.使用tf.data创建自定义数据集 我们使用kaggle上的猫狗数据以及tf.dat ...

  10. Mysql数据库分布式事务XA详解

    XA事务简介 XA 事务的基础是两阶段提交协议.需要有一个事务协调者来保证所有的事务参与者都完成了准备工作(第一阶段).如果协调者收到所有参与者都准备好的消息,就会通知所有的事务都可以提交了(第二阶段 ...