You are given a personal information string s, representing either an
email address or a phone number. Return themasked 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.
Input: s ="[email protected]"Output: "l*****[email protected]"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.
Input: s ="[email protected]"Output: "a*****[email protected]"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"is2 characters, it still must have 5 asterisks in the middle.
Input: s ="1(234)567-890"Output: "***-***-7890"Explanation: s is a phone number.There are 10 digits, so the local number is10 digits and the country code is0 digits.Thus, the resulting masked number is"***-***-7890".
The problem is about masking either an email or a phone number. For emails, we mask the middle part of the name and lowercase everything. For phone numbers, we keep only digits, mask all but the last 4, and format according to the number of digits.
classSolution {
public String maskPII(String s) {
if (s.contains("@")) {
s = s.toLowerCase();
int at = s.indexOf('@');
return s.charAt(0) +"*****"+ s.charAt(at-1) + s.substring(at);
} else {
StringBuilder digits =new StringBuilder();
for (char c : s.toCharArray()) if (Character.isDigit(c)) digits.append(c);
String local ="***-***-"+ digits.substring(digits.length()-4);
if (digits.length() == 10) return local;
String country ="+"+"*".repeat(digits.length()-10) +"-";
return country + local;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funmaskPII(s: String): String {
if ("@"in s) {
val t = s.lowercase()
val at = t.indexOf('@')
return t[0] + "*****" + t[at-1] + t.substring(at)
} else {
val digits = s.filter { it.isDigit() }
val local = "***-***-" + digits.takeLast(4)
returnif (digits.length ==10) local else"+" + "*".repeat(digits.length-10) + "-" + local
}
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defmaskPII(self, s: str) -> str:
if'@'in s:
s = s.lower()
name, domain = s.split('@')
return name[0] +'*****'+ name[-1] +'@'+ domain
digits = [c for c in s if c.isdigit()]
local ='***-***-'+''.join(digits[-4:])
if len(digits) ==10:
return local
return'+'+'*'* (len(digits)-10) +'-'+ local
impl Solution {
pubfnmask_pii(s: String) -> String {
if s.contains('@') {
let s = s.to_lowercase();
let at = s.find('@').unwrap();
let bytes = s.as_bytes();
letmut res = String::new();
res.push(bytes[0] aschar);
res.push_str("*****");
res.push(bytes[at-1] aschar);
res.push_str(&s[at..]);
res
} else {
let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
let local =format!("***-***-{}", &digits[digits.len()-4..]);
if digits.len() ==10 {
return local;
}
let country =format!("+{}-", "*".repeat(digits.len()-10));
format!("{}{}", country, local)
}
}
}