为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode831. 隐藏个人信息 | Masking Personal Information

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 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.

给你一条个人信息 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 }

 

posted @ 2019-03-28 17:33  为敢技术  阅读(479)  评论(0编辑  收藏  举报