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:
- Email addresses:
- Lowercase all the names.
- Mask all characters of the first name except the first and last.
- Maintain the domain name as is.
Example: “LeetCode@leetcode.com” -> “l*****e@leetcode.com”
- Phone numbers:
- Remove all non-digit characters and mask all digits except the last four.
- Add a country code if applicable, which can be inferred from the length of the digits.
- Use the format
"***-***-XXXX" where ‘XXXX’ are the last four digits.
- If there are more digits, prepend the country code in the format
"+C-***-***-XXXX" where C is the number of digits the country code has.
Example: “1(234)567-890” -> “--7890”
“+86 (10)12345678” -> “+--**-5678”
Clarifying Questions
- Email Characters: Does the email domain need to be case preserved?
- No, the entire email should be lowercased.
- Phone Number Optional Characters: Are we allowed to have spaces or other optional characters in the resultant phone number?
- No, the resultant phone number format should strictly follow the standard, only containing “-“ as the delimiter.
Strategy
- Identify Type of Data:
- Check if the input contains an “@” symbol to determine if it is an email.
- Email Masking:
- Convert everything to lowercase.
- Split the email into the local part and the domain.
- Mask the local part by keeping the first and last characters visible and replacing other middle characters with “*****”.
- Phone Number Masking:
- Extract all digits from the input string.
- Mask all digits except the last four.
- Determine the country code based on the number of digits.
- Format it accordingly, with or without the country code.
Code
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"
Time Complexity
- For Emails: The steps involve a couple of string operations (lowercase conversion, split, and concatenation), resulting in an O(n) complexity where n is the length of the email string.
- For Phone Numbers: Filtering out digits and formatting is straightforward with a complexity of O(n) where n is the length of the input string containing the phone number.
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?