Masking Personal Information
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:
- Emails have length at least 8.
- Phone numbers have length at least 10.
- 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的更多相关文章
- 【LeetCode】831. Masking Personal Information 解题报告(Python)
[LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- [LeetCode] Masking Personal Information 给个人信息打码
We are given a personal information string S, which may represent either an email address or a phone ...
- [Swift]LeetCode831. 隐藏个人信息 | Masking Personal Information
We are given a personal information string S, which may represent either an email address or a phone ...
- 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 ...
- 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 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)
Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
随机推荐
- 故意使用free掉的内存的一个实验( 常量区/栈)
故意使用free掉的内存的一个实验 考虑一下两种声明 struct stuff{ char home[10]; int num; char name[10]; }; struct stuff{ cha ...
- 简化ETL工作,编写一个Canal胶水层
前提 这是一篇憋了很久的文章,一直想写,却又一直忘记了写.整篇文章可能会有点流水账,相对详细地介绍怎么写一个小型的"框架".这个精悍的胶水层已经在生产环境服役超过半年,这里尝试把耦 ...
- 034 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 01 流程控制概述
034 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 01 流程控制概述 本文知识点:Java中的流程控制相关概念的认识 三大流程控制语句结构的简介 顺序 ...
- matlab中get查询图形对象属性
来源:https://ww2.mathworks.cn/help/matlab/ref/get.html?searchHighlight=get&s_tid=doc_srchtitle get ...
- ESP8266 玩板记
一.前言 esp8266的玩板记,后面应该会去更一些其他东西,这一块内容,这算是收官之战了. IoT,江湖有缘再相会 二.ESP8266实现WiFi杀手/钓鱼 这次的博客做的是一个娱乐性较强的项目. ...
- shell-的变量-全局变量
shell变量基础及深入 1. 变量类型 变量可分为两类:环境变量(全局变量)和局部变量. 环境变量也可称为全局变量,可以在创建他们的shell及其派生出来的任意子进程shell中使用.局部变量只 ...
- OpenCV计算机视觉学习(4)——图像平滑处理(均值滤波,高斯滤波,中值滤波,双边滤波)
如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice &q ...
- 从面试角度学完 Kafka
Kafka 是一个优秀的分布式消息中间件,许多系统中都会使用到 Kafka 来做消息通信.对分布式消息系统的了解和使用几乎成为一个后台开发人员必备的技能.今天码哥字节就从常见的 Kafka 面试题入手 ...
- Python 面向对象(1): 类方法基础
# 类方法 # 如果 该class 没有要继承的类 则一般需要继承 object 基类 class ClassMethodBase(object): # 起手初始化 以示尊敬 def __init__ ...
- centos8平台使用lscpu查看cpu信息
一,lscpu所属的包: [root@yjweb ~]# whereis lscpu lscpu: /usr/bin/lscpu /usr/share/man/man1/lscpu.1.gz [roo ...