[LeetCode] 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:
S.length <= 40
.- Emails have length at least 8.
- Phone numbers have length at least 10.
这道题让我们给个人信息打码,在这个注重保护隐私的时代,打码什么的都是家常便饭了,想看高清无码的,那就得掏钱。这里对邮箱和电话分别进行了不同的打码方式,对于邮箱来说,只保留用户名的首尾两个字符,然后中间固定加上五个星号,还有就是所有的字母转小写。对于电话来说,有两种方式,有和没有国家代号,有的话其前面必须有加号,跟后面的区域号用短杠隔开,后面的10个电话号分为三组,个数分别为3,3,4。每组之间还是用短杠隔开,除了最后一组的数字保留之外,其他的都用星号代替。弄清楚了题意,就开始解题吧。既然是字符串的题目,那么肯定要遍历这个字符串了,我们关心的主要是数字和字母,所以要用个变量str来保存遍历到的数字和字母,所以判断,如果是数字或者小写字母的话,直接加入str中,若是大写字母的话,转成小写字母再加入str,如果遇到了 ‘@’ 号,那么表示当前处理的是邮箱,而此时的用户已经全部读入str了,那直接就取出首尾字符,然后中间加五个星号,并再加上 ‘@’ 号存入结果res中,并把str清空。若遇到了点,说明此时是邮箱的后半段,因为题目中限制了用户名中不会有点存在,那么我们将str和点一起加入结果res,并将str清空。当遍历结束时,若此时结果res不为空,说明我们处理的是邮箱,那么返回结果res加上str,因为str中存的是 "com",还没有来得及加入结果res。若res为空,说明处理的是电话号码,所有的数字都已经加入了str,由于国家代号可能有也可能没有,所以判断一下存入的数字的个数,如果超过10个了,说明有国家代号,那么将超过的部分取出来拼装一下,前面加 ‘+’ 号,后面加短杠。然后再将10位号码的前六位的星号格式加上,并加上最后四个数字即可,参见代码如下:
解法一:
class Solution {
public:
string maskPII(string S) {
string res = "", str = "";
for (char c : S) {
if (c >= 'a' && c <= 'z') str.push_back(c);
else if (c >= 'A' && c <= 'Z') str.push_back(c + );
else if (c >= '' && c <= '') str.push_back(c);
else if (c == '@') {
res += string(, str[]) + "*****" + string(, str.back()) + "@";
str.clear();
} else if (c == '.') {
res += str + ".";
str.clear();
}
}
if (!res.empty()) return res + str;
int n = str.size();
if (n > ) res += "+" + string(n - , '*') + "-";
res += "***-***-" + str.substr(n - );
return res;
}
};
我们还可以换一种写法,首先在字符串S中找 ‘@’ 号,这是区分邮箱地址和电话号码的关键所在。若没找到,则按电话号码的方法处理,跟上面的操作几乎相同。若找到了,则直接取出第一个字母,加五个星号,然后将 ‘@’ 号位置及其后面所有的字符都加入结果res,然后再统一转为小写即可,参见代码如下:
解法二:
class Solution {
public:
string maskPII(string S) {
string res = "", str = "";
auto pos = S.find('@');
if (pos == string::npos) {
for (char c : S) {
if (c >= '' && c <= '') str.push_back(c);
}
int n = str.size();
if (n > ) res += "+" + string(n - , '*') + "-";
res += "***-***-" + str.substr(n - );
} else {
res = S.substr(, ) + "*****" + S.substr(pos - );
transform(res.begin(), res.end(), res.begin(), ::tolower);
}
return res;
}
};
参考资料:
https://leetcode.com/problems/masking-personal-information/
https://leetcode.com/problems/masking-personal-information/discuss/129400/Straightforward-C%2B%2B
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Masking Personal Information 给个人信息打码的更多相关文章
- 【LeetCode】831. Masking Personal Information 解题报告(Python)
[LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- Masking Personal Information
Masking Personal Information We are given a personal information string S, which may represent eithe ...
- [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 ...
- Http Header信息&状态码
Header信息 (Status-Line):状态项,包括协议类型,http返回码和状态: Cache-control:是否可以被缓存(public可以:private和no-cache不可以: ...
- C# 使用 Abot 实现 爬虫 抓取网页信息 源码下载
下载地址 ** dome **
- error错误信息状态码含义
XMLHttpRequest.status: 200:成功. 401:拒绝访问. 403:禁止访问. 404:找不到. 405:方法不被允许. 407:要求进行代理身份验证. 500:内部服务器错误. ...
- Java实现 LeetCode 709 转换成小写字母(ASCII码处理)
709. 转换成小写字母 实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串. 示例 1: 输入: "Hell ...
随机推荐
- 传输层--TCP和UDP的区别
UDP(用户数据报协议):为调用它的应用程序提供了一种不可靠.无连接的服务. TCP(传输控制协议):为调用它的应用程序提供了一种可靠的.面向连接的服务. 当设计一个网络应用程序时,该应用程序的开发人 ...
- 2019 icpc南昌全国邀请赛-网络选拔赛J题 树链剖分+离线询问
链接:https://nanti.jisuanke.com/t/38229 题意: 给一棵树,多次查询,每次查询两点之间权值<=k的边个数 题解: 离线询问,树链剖分后bit维护有贡献的位置即可 ...
- linux 安装虚拟机
如果虚拟机创建不了就重启电脑 重启时 按下F2 出现后 第二个 往下 有个默认的 那个那个 打开虚拟机 选择第一个 然后是选择语言选择软件里面的 软件选择选择 基本网页服务器(右侧选择 python ...
- [Linux]信号集和sigprocmask信号屏蔽函数
一.概述 系统提供这样一种能力,就是创建一个信号集,然后传递给信号屏蔽函数,从而屏蔽向该进程发送的信号. 有一点需要注意的是,不能屏蔽SIGKILL和SIGSTOP信号. 信号集是sigset_t类型 ...
- JAVA 类的定义(定义一个类,来模拟“学生”)
package Code413;/*定义一个类,来模拟“学生”属性 (是什么) 姓名 年龄行为(能做什么) 吃饭 睡觉 学习对应到Java的类当中 成员变量(属性) String nanme; //姓 ...
- STM32F0使用LL库实现Modbus通讯
在本次项目中,限于空间要求我们选用了STM32F030F4作为控制芯片.这款MCU不但封装紧凑,而且自带的Flash空间也非常有限,所以我们选择了LL库实现.本篇将说明基于LL实现USART通讯. 1 ...
- Vue.js学习笔记(介绍)
Vue语法也可以进行APP开发,需要借助weex Vue.js是一套构建用户界面的框架,只关注视图层,便于与第三方库或既有项目整合. 在Vue中的核心概念:让用户不能操作Dom元素(减少不必要的dom ...
- 主席树套树状数组——带修区间第k大zoj2112
主席树带修第k大 https://www.cnblogs.com/Empress/p/4659824.html 讲的非常好的博客 首先按静态第k大建立起一组权值线段树(主席树) 然后现在要将第i个值从 ...
- 2018-2019-2 网络对抗技术 20165328 Exp4 恶意代码分析
实验内容: 系统运行监控(2分) 使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间并分析该文件,综述一下分析结果.目标就是找出所有连网的程序,连了哪里,大约 ...
- IntelliJ IDEA 创建 Java包
一.创建包 1.在已有项目的"src"文件夹 -> 右键 -> New -> Package 2.命名包名,注意命名规范 二.创建类 1.新建包成功之后,在包上右 ...