Leetcode’s problem 831, “Masking Personal Information,” involves creating a function to mask personal information. The masked information can be either an email address or a phone number, depending on the format of the provided string.
The rules are:
Example: “LeetCode@leetcode.com” -> “l*****e@leetcode.com”
"***-***-XXXX"
where ‘XXXX’ are the last four digits."+C-***-***-XXXX"
where C is the number of digits the country code has.Example: “1(234)567-890” -> “--7890” “+86 (10)12345678” -> “+--**-5678”
def maskPII(s: str) -> str:
if '@' in s:
# Email case
name, domain = s.lower().split('@')
return name[0] + "*****" + name[-1] + "@" + domain
else:
# Phone number case
digits = [char for char in s if char.isdigit()]
num_digits = len(digits)
local = "***-***-" + ''.join(digits[-4:])
if num_digits == 10:
return local
else:
country_code = "+" + "*" * (num_digits - 10)
return country_code + "-" + local
## Examples
# Testing the function with examples provided:
print(maskPII("LeetCode@LeetCode.com")) # Output: "l*****e@leetcode.com"
print(maskPII("1(234)567-890")) # Output: "***-***-7890"
print(maskPII("+86 (10)12345678")) # Output: "+**-***-***-5678"
Overall, both scenarios operate in linear time relative to the length of the input string.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?