原文链接: https://leetcode-cn.com/problems/masking-personal-information
英文原文
You are given a personal information string s, representing either an email address or a phone number. Return the masked personal information using the below rules.
Email address:
An email address is:
- A name consisting of uppercase and lowercase English letters, followed by
- The
'@'symbol, followed by - The domain consisting of uppercase and lowercase English letters with a dot
'.'somewhere in the middle (not the first or last character).
To mask an email:
- The uppercase letters in the name and domain must be converted to lowercase letters.
- The middle letters of the name (i.e., all but the first and last letters) must be replaced by 5 asterisks
"*****".
Phone number:
A phone number is formatted as follows:
- The phone number contains 10-13 digits.
- The last 10 digits make up the local number.
- The remaining 0-3 digits, in the beginning, make up the country code.
- Separation characters from the set
{'+', '-', '(', ')', ' '}separate the above digits in some way.
To mask a phone number:
- Remove all separation characters.
- The masked phone number should have the form:
"***-***-XXXX"if the country code has 0 digits."+*-***-***-XXXX"if the country code has 1 digit."+**-***-***-XXXX"if the country code has 2 digits."+***-***-***-XXXX"if the country code has 3 digits.
"XXXX"is the last 4 digits of the local number.
Example 1:
Input: s = "LeetCode@LeetCode.com" Output: "l*****e@leetcode.com" Explanation: s is an email address. The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
Example 2:
Input: s = "AB@qq.com" Output: "a*****b@qq.com" Explanation: s is an email address. The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks. Note that even though "ab" is 2 characters, it still must have 5 asterisks in the middle.
Example 3:
Input: s = "1(234)567-890" Output: "***-***-7890" Explanation: s is a phone number. There are 10 digits, so the local number is 10 digits and the country code is 0 digits. Thus, the resulting masked number is "***-***-7890".
Example 4:
Input: s = "86-(10)12345678" Output: "+**-***-***-5678" Explanation: s is a phone number. There are 12 digits, so the local number is 10 digits and the country code is 2 digits. Thus, the resulting masked number is "+**-***-***-7890".
Constraints:
sis either a valid email or a phone number.- If
sis an email:8 <= s.length <= 40sconsists of uppercase and lowercase English letters and exactly one'@'symbol and'.'symbol.
- If
sis a phone number:10 <= s.length <= 20sconsists of digits, spaces, and the symbols'(',')','-', and'+'.
中文题目
给你一条个人信息字符串 s ,可能表示一个 邮箱地址 ,也可能表示一串 电话号码 。返回按如下规则 隐藏 个人信息后的结果:
电子邮件地址:
一个电子邮件地址由以下部分组成:
- 一个 名字 ,由大小写英文字母组成,后面跟着
- 一个
'@'字符,后面跟着 - 一个 域名 ,由大小写英文字母和一个位于中间的
'.'字符组成。'.'不会是域名的第一个或者最后一个字符。
要想隐藏电子邮件地址中的个人信息:
- 名字 和 域名 部分的大写英文字母应当转换成小写英文字母。
- 名字 中间的字母(即,除第一个和最后一个字母外)必须用 5 个
"*****"替换。
电话号码:
一个电话号码应当按下述格式组成:
- 电话号码可以由 10-13 位数字组成
- 后 10 位构成 本地号码
- 前面剩下的 0-3 位,构成 国家代码
- 利用
{'+', '-', '(', ')', ' '}这些 分隔字符 按某种形式对上述数字进行分隔
要想隐藏电话号码中的个人信息:
- 移除所有 分隔字符
- 隐藏个人信息后的电话号码应该遵从这种格式:
"***-***-XXXX"如果国家代码为 0 位数字"+*-***-***-XXXX"如果国家代码为 1 位数字"+**-***-***-XXXX"如果国家代码为 2 位数字"+***-***-***-XXXX"如果国家代码为 3 位数字
"XXXX"是最后 4 位 本地号码
示例 1:
输入:s = "LeetCode@LeetCode.com" 输出:"l*****e@leetcode.com" 解释:s 是一个电子邮件地址。 名字和域名都转换为小写,名字的中间用 5 个 * 替换。
示例 2:
输入:s = "AB@qq.com" 输出:"a*****b@qq.com" 解释:s 是一个电子邮件地址。 名字和域名都转换为小写,名字的中间用 5 个 * 替换。 注意,尽管 "ab" 只有两个字符,但中间仍然必须有 5 个 * 。
示例 3:
输入:s = "1(234)567-890" 输出:"***-***-7890" 解释:s 是一个电话号码。 共计 10 位数字,所以本地号码为 10 位数字,国家代码为 0 位数字。 因此,隐藏后的电话号码应该是 "***-***-7890" 。
示例 4:
输入:s = "86-(10)12345678" 输出:"+**-***-***-5678" 解释:s 是一个电话号码。 共计 12 位数字,所以本地号码为 10 位数字,国家代码为 2 位数字。 因此,隐藏后的电话号码应该是 "+**-***-***-7890" 。
提示:
s是一个 有效 的电子邮件或者电话号码- 如果
s是一个电子邮件:8 <= s.length <= 40s是由大小写英文字母,恰好一个'@'字符,以及'.'字符组成
- 如果
s是一个电话号码:10 <= s.length <= 20s是由数字、空格、字符'('、')'、'-'和'+'组成
通过代码
官方题解
方法一:模拟
我们首先判断 S 是邮箱还是电话号码。显然,如果 S 中有字符 '@',那么它是邮箱,否则它是电话号码。
如果 S 是邮箱,我们将 S 的 '@' 之前的部分保留第一个和最后一个字符,中间用 '*****' 代替,并将整个字符串转换为小写。
如果 S 是电话号码,我们只保留 S 中的所有数字。首先将最后 10 位本地号码变成 '***-***-abcd' 的形式,再判断 S 中是否有额外的国际号码。如果有,则将国际号码之前添加 '+' 号并加到本地号码的最前端。
[sol1]class Solution { public String maskPII(String S) { int atIndex = S.indexOf('@'); if (atIndex >= 0) { // email return (S.substring(0, 1) + "*****" + S.substring(atIndex - 1)).toLowerCase(); } else { // phone String digits = S.replaceAll("\\D+", ""); String local = "***-***-" + digits.substring(digits.length() - 4); if (digits.length() == 10) return local; String ans = "+"; for (int i = 0; i < digits.length() - 10; ++i) ans += "*"; return ans + "-" + local; } } }
[sol1]class Solution(object): def maskPII(self, S): if '@' in S: #email first, after = S.split('@') return "{}*****{}@{}".format( first[0], first[-1], after).lower() else: #phone digits = filter(unicode.isdigit, S) local = "***-***-{}".format(digits[-4:]) if len(digits) == 10: return local return "+{}-".format('*' * (len(digits) - 10)) + local
复杂度分析
时间复杂度:$O(N)$,其中 $N$ 是字符串
S的长度。空间复杂度:$O(1)$。
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 7822 | 19258 | 40.6% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|