★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10616498.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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 ≥ 2consisting 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 thefirst 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.

给你一条个人信息 string S,它可能是一个邮箱地址,也可能是一个电话号码。

我们将隐藏它的隐私信息,通过如下规则:

1. 电子邮箱

定义名称 <name> 的长度大于2,并且只包含小写字母 a-z 和大写字母 A-Z。

电子邮箱地址由名称 <name> 开头,紧接着是符号 '@',后面接着一个名称 <name>,再接着一个点号 '.',然后是一个名称 <name>。

电子邮箱地址确定为有效的,并且格式是"name1@name2.name3"。

为了隐藏电子邮箱,所有的名称 <name> 必须被转换成小写的,并且第一个名称 <name> 的第一个字母和最后一个字母的中间的所有字母由 5 个 '*' 代替。

2. 电话号码

电话号码是一串包括数组 0-9,以及 {'+', '-', '(', ')', ' '} 这几个字符的字符串。你可以假设电话号码包含 10 到 13 个数字。

电话号码的最后 10 个数字组成本地号码,在这之前的数字组成国际号码。注意,国际号码是可选的。我们只暴露最后 4 个数字并隐藏所有其他数字。

本地号码是有格式的,并且如 "***-***-1111" 这样显示,这里的 1 表示暴露的数字。

为了隐藏有国际号码的电话号码,像 "+111 111 111 1111",我们以 "+***-***-***-1111" 的格式来显示。在本地号码前面的 '+' 号和第一个 '-' 号仅当电话号码中包含国际号码时存在。例如,一个 12 位的电话号码应当以 "+**-" 开头进行显示。

注意:像 "(",")"," " 这样的不相干的字符以及不符合上述格式的额外的减号或者加号都应当被删除。

最后,将提供的信息正确隐藏后返回。

示例 1:

输入: "LeetCode@LeetCode.com"
输出: "l*****e@leetcode.com"
解释:
所有的名称转换成小写, 第一个名称的第一个字符和最后一个字符中间由 5 个星号代替。
因此,"leetcode" -> "l*****e"。

示例 2:

输入: "AB@qq.com"
输出: "a*****b@qq.com"
解释:
第一个名称"ab"的第一个字符和最后一个字符的中间必须有 5 个星号
因此,"ab" -> "a*****b"。

示例 3:

输入: "1(234)567-890"
输出: "***-***-7890"
解释:
10 个数字的电话号码,那意味着所有的数字都是本地号码。

示例 4:

输入: "86-(10)12345678"
输出: "+**-***-***-5678"
解释:
12 位数字,2 个数字是国际号码另外 10 个数字是本地号码 。

注意:

  1. S.length <= 40
  2. 邮箱的长度至少是 8。
  3. 电话号码的长度至少是 10。

Runtime: 8 ms
Memory Usage: 20.2 MB
 1 class Solution {
 2     var country:[String] = ["", "+*-", "+**-", "+***-"]
 3     func maskPII(_ S: String) -> String {
 4         var S = S
 5         var at:Int = find(S,"@")
 6         if at > 0
 7         {
 8             S = S.lowercased
 9             return (String(S[0]) + "*****" + S.subString(at - 1)).lowercased;
10         }
11         var arr:[Character] = Array(S)
12         for i in (0..<arr.count).reversed()
13         {
14             if arr[i] < "0" || arr[i] > "9"
15             {
16                 arr.remove(at:i)
17             }
18         }
19         S = String(arr)
20         return country[S.count - 10] + "***-***-" + S.subString(S.count - 4)
21     }
22
23     func find(_ S:String,_ char:Character) -> Int
24     {
25         var arrS:[Character] = Array(S)
26         for i in 0..<arrS.count
27         {
28             if arrS[i] == char
29             {
30                 return i
31             }
32         }
33         return -1
34     }
35 }
36
37 //String扩展
38 extension String {
39     //subscript函数可以检索数组中的值
40     //直接按照索引方式截取指定索引的字符
41     subscript (_ i: Int) -> Character {
42         //读取字符
43         get {return self[index(startIndex, offsetBy: i)]}
44     }
45
46     // 截取字符串:从index到结束处
47     // - Parameter index: 开始索引
48     // - Returns: 子字符串
49     func subString(_ index: Int) -> String {
50         let theIndex = self.index(self.endIndex, offsetBy: index - self.count)
51         return String(self[theIndex..<endIndex])
52     }
53 }


Runtime: 8 ms
Memory Usage: 20.7 MB

 1 class Solution {
 2     func maskPII(_ S: String) -> String {
 3         if S.contains("@") {
 4             let name1 = String(S.split(separator: "@").first!)
 5             let name2 = String(S.split(separator: "@").last!)
 6             let res = String(name1.first!).lowercased() + "*****" + String(name1.last!).lowercased() + "@" + name2.lowercased()
 7             return res
 8         } else {
 9             var res = S
10
11             if S.contains("+") {
12                res = res.replacingOccurrences(of: "+", with: "")
13             }
14             if S.contains("-") {
15                 res = res.replacingOccurrences(of: "-", with: "")
16             }
17             if S.contains("(") {
18                 res = res.replacingOccurrences(of: "(", with: "")
19             }
20             if S.contains(")") {
21                 res = res.replacingOccurrences(of: ")", with: "")
22             }
23             if S.contains(" ") {
24                 res = res.replacingOccurrences(of: " ", with: "")
25             }
26
27             if res.count == 10 {
28                 res = "***-***-" + String(res.suffix(4))
29             } else if res.count == 11 {
30                 res = "+*-***-***-" + String(res.suffix(4))
31             } else if res.count == 13 {
32                 res = "+***-***-***-" + String(res.suffix(4))
33             } else {
34               res = "+**-***-***-" + String(res.suffix(4))
35             }
36             return res
37         }
38     }
39 }


19888kb

 1 class Solution {
 2     func maskPII(_ S: String) -> String {
 3         let emailArr = S.split(separator: "@")
 4         if emailArr.count == 2 {
 5             var prefix = emailArr[0].lowercased()
 6             return String(prefix.first!) + "*****" + String(prefix.last!) + "@" + emailArr[1].lowercased()
 7         }
 8
 9         var pureDigtals = [Character]()
10         for c in S {
11             if c == "-" || c == "(" || c == ")" || c == "+" ||  c == " " {
12                 continue
13             }
14             pureDigtals.append(c)
15         }
16
17         var lastFourStr = String(pureDigtals.suffix(4))
18
19         if pureDigtals.count == 10 {
20             return  "***-***-" + lastFourStr
21         }
22         var starStr = "+"
23         for i in 0..<(pureDigtals.count - 10) {
24             starStr += "*"
25         }
26         return starStr + "-***-***-" + lastFourStr
27     }
28 }

转载于:https://www.cnblogs.com/strengthen/p/10616498.html

[Swift]LeetCode831. 隐藏个人信息 | Masking Personal Information相关推荐

  1. LeetCode 831. Masking Personal Information【字符串,正则表达式】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12.由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止:由于LeetCode还在不断地创建新 ...

  2. 17.2: Apps that require users to share personal information, such as email address and date of birth

    最近提交的一版应用,出现了 17.2: Apps that require users to share personal information, such as email address and ...

  3. 信息级联/信息瀑布(Information Cascade)

    原文链接: 关于信息级联的看法_计算社会学_新浪博客 http://blog.sina.com.cn/s/blog_12bcdd96c0102xvcs.html 关于信息级联(information ...

  4. EXCEL2013保存时提示Be careful!Parts of your document may include personal information...

    在EXCEL 2013中创建VBA宏后,保存时出现如下提示: 虽不影响保存结果,但是每次提示让人心烦,GOOGLE发现是因为有个检测功能造成的,关闭此功能路径如下: File->Options ...

  5. Nginx隐藏主机信息,proxy_hide_header 与fastcgi_hide_header

    Nginx中proxy_hide_header 与fastcgi_hide_header都可以隐藏主机头信息,两者在具体使用时还是有着一定的区别的.刚好业务使用的nginx反向代理在显示响应头时将后端 ...

  6. linux隐藏apache信息,Apache防盗链和隐藏版本信息-linux-centos运维

    有需要服务器方面的需求和咨询,可以联系博主 QQ 7271895 一.防盗链 二.隐藏版本信息 实验要求: 三台虚拟机分别是:linux和两台windows虚拟机,linux虚拟机为服务器,Windo ...

  7. apache php隐藏头信息的方法,apache、php隐藏http头部版本信息的实现方法

    1.apache隐藏头部版本信息,编辑httpd.conf文件,找到: ServerTokens OS ServerSignature On 修改为: ServerTokens ProductOnly ...

  8. 软件LoadRunner 产品信息(product information)

    我用的虚拟机里边装的LR.关于产品信息(product information),产品名称(product name),组件名称(Component Name) 转载于:https://www.cnb ...

  9. 微信 发送图片 服务器上,公安提醒:微信发照片,千万别传原图”!5个步骤教你隐藏位置信息...

    原标题:公安提醒:微信发照片,千万别传"原图"!5个步骤教你隐藏位置信息 你与陌生人之间,可能只是一张照片的距离.有时候一张"原图"照片,分分钟就暴露了你的信息 ...

  10. Cesium隐藏版权信息

    Cesium隐藏版权信息 CesiumWidget.css中: .cesium-widget-credits{ display:none}: Viewer.css中: .cesium-viewer . ...

最新文章

  1. 互联网公司的那些搞笑gif
  2. c#之旅--第六天(类,对象,方法)
  3. 绝地求生自定义服务器租用,绝地求生自定义服务器怎么开 自定义服务器设置方法...
  4. python是什么编程教程-编程零基础应当如何开始学习 Python?
  5. 老司机也晕车--java字符串String晕车之旅
  6. Linux/Unix 新手和专家教程
  7. PostgreSQL备份还原
  8. [转]Java游戏引擎
  9. could not read data from '/Users/xxxx/myapp-Info.plist'
  10. jquery显示隐藏切换_jQuery显示,隐藏,切换
  11. 死磕算法!35篇算法设计实例+6本必读书打包送你
  12. 起底“XX神器”:超级手机病毒的因果
  13. python mql4跟单_MT4软件本地跟单方法的实现 -
  14. 微信公众号菜单栏链接开发
  15. python 处理 Excel 表格
  16. Spring框架-JdbcTemplate
  17. 30 张图带你揭秘 CPU 是如何制造出来的!
  18. eclipse出现Parameter index out of range (1 number of parameters, which is 0)报错
  19. 企业小程序应该如何开发?
  20. x86汇编_指令集大全_笔记_6

热门文章

  1. java-idea-3批处理文件运行jar包
  2. aliy 数据库连接池加密_Druid数据库连接池 实现数据库账号密码加密
  3. python监听键盘输入_Python监听鼠标键盘事件
  4. DAHON 美国大行
  5. 手挽手带你学VUE:四档 Vue-cli3 Vuex Vue-router
  6. 基础教程——python函数
  7. linux命令文本处理(一)grep
  8. ural 1112,LIS
  9. Android碎碎念 -- 广播LocalBroadcastManager的实现
  10. HttpServletRequest中文乱码