在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ We are given a personal information string We would like to mask this personal information according to the following rules:
We define a name to be a string of An email address starts with a name, followed by the symbol All email addresses are guaranteed to be valid and in the format of 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
A phone number is a string consisting of only the 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 To mask a phone number with country code like Note that extraneous characters like
Return the correct "mask" of the information provided.
Example 1: Input: "[email protected]" Output: "l*****[email protected]" 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: "[email protected]" Output: "a*****[email protected]" 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:
给你一条个人信息 string 我们将隐藏它的隐私信息,通过如下规则: 1. 电子邮箱 定义名称 <name> 的长度大于2,并且只包含小写字母 a-z 和大写字母 A-Z。 电子邮箱地址由名称 <name> 开头,紧接着是符号 '@',后面接着一个名称 <name>,再接着一个点号 '.',然后是一个名称 <name>。 电子邮箱地址确定为有效的,并且格式是"[email protected]"。 为了隐藏电子邮箱,所有的名称 <name> 必须被转换成小写的,并且第一个名称 <name> 的第一个字母和最后一个字母的中间的所有字母由 5 个 '*' 代替。 2. 电话号码 电话号码是一串包括数组 0-9,以及 {'+', '-', '(', ')', ' '} 这几个字符的字符串。你可以假设电话号码包含 10 到 13 个数字。 电话号码的最后 10 个数字组成本地号码,在这之前的数字组成国际号码。注意,国际号码是可选的。我们只暴露最后 4 个数字并隐藏所有其他数字。 本地号码是有格式的,并且如 "***-***-1111" 这样显示,这里的 1 表示暴露的数字。 为了隐藏有国际号码的电话号码,像 "+111 111 111 1111",我们以 "+***-***-***-1111" 的格式来显示。在本地号码前面的 '+' 号和第一个 '-' 号仅当电话号码中包含国际号码时存在。例如,一个 12 位的电话号码应当以 "+**-" 开头进行显示。 注意:像 "(",")"," " 这样的不相干的字符以及不符合上述格式的额外的减号或者加号都应当被删除。
最后,将提供的信息正确隐藏后返回。
示例 1: 输入: "[email protected]" 输出: "l*****[email protected]" 解释: 所有的名称转换成小写, 第一个名称的第一个字符和最后一个字符中间由 5 个星号代替。 因此,"leetcode" -> "l*****e"。 示例 2: 输入: "[email protected]" 输出: "a*****[email protected]" 解释: 第一个名称"ab"的第一个字符和最后一个字符的中间必须有 5 个星号 因此,"ab" -> "a*****b"。 示例 3: 输入: "1(234)567-890" 输出: "***-***-7890" 解释: 10 个数字的电话号码,那意味着所有的数字都是本地号码。 示例 4: 输入: "86-(10)12345678" 输出: "+**-***-***-5678" 解释: 12 位数字,2 个数字是国际号码另外 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 }
|
请发表评论