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

时间:2023-02-11 00:27:29

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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/masking-personal-information/description/

题目描述:

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. S.length <= 40.
  2. Emails have length at least 8.
  3. Phone numbers have length at least 10.

题目大意

题目这么长的,应该还是第一次见。

本题的意思是隐藏手机号/邮箱的部分数字。

隐藏邮箱的规则是:全部转成小写,把用户名改成只保留首尾字母,并且两个字母之间用5个*填充

隐藏手机号的规则是:手机号的后10位是本地号码,其余的前面的数字是区号。本地号码直接转成"***-***-末尾四位"格式。区号变成"+区号位数-"

解题方法

没什么好解释的,看完题就行。

class Solution(object):
def convert_phone(self, phone):
phone = phone.strip().replace(' ', '').replace('(', '').replace(')', '').replace('-', '').replace('+', '')
if len(phone) == 10:
return "***-***-" + phone[-4:]
else:
return "+" + '*' * (len(phone) - 10) + "-***-***-" + phone[-4:] def convert_email(self, email):
email = email.lower()
first_name, host = email.split('@')
return first_name[0] + '*****' + first_name[-1] + '@' + host def maskPII(self, S):
"""
:type S: str
:rtype: str
"""
return self.convert_email(S) if '@' in S else self.convert_phone(S)

日期

2018 年 6 月 10 日 ———— 等了两天的腾讯比赛复赛B的数据集,结果人家在复赛刚开始就给了。。

【LeetCode】831. Masking Personal Information 解题报告(Python)的更多相关文章

  1. 【LeetCode】62&period; Unique Paths 解题报告&lpar;Python & C&plus;&plus;&rpar;

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  2. 【LeetCode】376&period; Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. 【LeetCode】649&period; Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  4. 【LeetCode】911&period; Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  5. 【LeetCode】886&period; Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  6. 【LeetCode】36&period; Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  7. 【LeetCode】870&period; Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  8. 【LeetCode】593&period; Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】435&period; Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

随机推荐

  1. HDU 1174 爆头&lpar;计算几何&rpar;

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1174 解题报告:就是用到了三维向量的点积来求点到直线的距离,向量(x1,y1,z1)与(x2,y2,z ...

  2. 【树莓派】树莓派网络配置:静态IP、无线网络、服务等

    一.网络配置之静态IP: 树莓派的默认网络为: haochuang@raspberrypi:~ $ vi /etc/network/interfaces # interfaces() file use ...

  3. ie8不支持transform&colon; translateY,ie9支持不友好

    transform: translateY(0);  ie8 不支持这个属性  但是我觉得用css实现的效果特好 特自然 也许是我的脚本写的不够完善 呵呵    现在我希望其他 主流的浏览器应用CSS ...

  4. Matlab神经网络工具箱学习之一

    1.神经网络设计的流程 2.神经网络设计四个层次 3.神经网络模型 4.神经网络结构 5.创建神经网络对象 6.配置神经网络的输入输出 7.理解神经网络工具箱的数据结构 8.神经网络训练 1.神经网络 ...

  5. LinkedHashMap介绍

    转载:http://uule.iteye.com/blog/1522291 jdk1.7API文档链接:http://tool.oschina.net/apidocs/apidoc?api=jdk_7 ...

  6. USB CCID协议和PC&sol;SC标准

    CCID是USB Chip/Smart Card Interface Devices,也就是USB芯片智能卡接口设备,是USB规范下的一种设备类型.就像HID设备一样,需要参考USB规范来写固件程序来 ...

  7. vue-底部导航栏

    <template> <div class="bottom"> <div class="bottom_button iconfont ico ...

  8. python 之 函数的参数

    函数的参数好几种类型:包括位置参数.默认参数.可变参数.关键字参数.命名关键字参数. 廖大神python学习笔记,大神网站:百度搜索“廖雪峰的官网” 1.位置参数:调用函数时根据函数定义的参数位置来传 ...

  9. Java-----隐藏手机号中间四位,身份证号码中间几位

    phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");152****4799 idCard.replace ...

  10. &period;Net core 应用程序发布Web时,有些文件夹没有发布成功解决办法

    如果文件是你在项目中手动添加的, 那么在解决方案中右击文件,然后点击属性,文件属性----高级----复制到输出目录----改为始终复制/如果较新则复制 即可.