You are given a personal information string s, representing either an email address or a phone number. Return the masked personal information using the below rules.
Email address:
An email address is:
'@' symbol, followed by'.' somewhere in the middle (not the first or last character).To mask an email:
"*****".Phone number:
A phone number is formatted as follows:
{'+', '-', '(', ')', ' '} separate the above digits in some way.To mask a phone number:
"***-***-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."XXXX" is the last 4 digits of the local number.Example 1:
Input: s = "LeetCode@LeetCode.com" Output: "l*****e@leetcode.com" 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.
Example 2:
Input: s = "AB@qq.com" Output: "a*****b@qq.com" 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" is 2 characters, it still must have 5 asterisks in the middle.
Example 3:
Input: s = "1(234)567-890" Output: "***-***-7890" Explanation: s is a phone number. There are 10 digits, so the local number is 10 digits and the country code is 0 digits. Thus, the resulting masked number is "***-***-7890".
Constraints:
s is either a valid email or a phone number.s is an email:
8 <= s.length <= 40s consists of uppercase and lowercase English letters and exactly one '@' symbol and '.' symbol.s is a phone number:
10 <= s.length <= 20s consists of digits, spaces, and the symbols '(', ')', '-', and '+'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method addresses masking personal information by trying every possible mask and format. This involves checking all potential combinations to find one that satisfies the rules for email and phone number masking. We check each option to comply with the specified formatting.
Here's how the algorithm would work step-by-step:
def mask_pii(personal_information): if '@' in personal_information: # Handle email masking. username, domain = personal_information.split('@') masked_username = username[0] + '*****' + username[-1] masked_email = masked_username + '@' + domain return masked_email.lower() else: # Handle phone number masking. digits_only = ''.join(character for character in personal_information if character.isdigit()) # We handle phone numbers that are 10 to 13 digits long. if len(digits_only) == 10: local_number = '***-***-' + digits_only[-4:] return '+*-' + local_number elif len(digits_only) == 11: country_code = '+' + digits_only[0] + '-' local_number = '***-***-' + digits_only[-4:] return country_code + '***-' + local_number elif len(digits_only) == 12: country_code = '+' + digits_only[:2] + '-' local_number = '***-***-' + digits_only[-4:] return country_code + '***-' + local_number else: # Length is 13 country_code = '+' + digits_only[:3] + '-' local_number = '***-***-' + digits_only[-4:] return country_code + '***-' + local_numberThe goal is to mask parts of an email address or phone number based on its type. We'll figure out what kind of information we have, then apply the right masking steps. By handling email and phone numbers differently and directly, we avoid unnecessary work.
Here's how the algorithm would work step-by-step:
def mask_personal_information(personal_info):
if '@' in personal_info:
# Handle email address
local_name, domain_name = personal_info.split('@')
masked_local_name = local_name[0] + "*****" + local_name[-1]
return masked_local_name + '@' + domain_name
else:
# Handle phone number
digits_only = ''.join(filter(str.isdigit, personal_info))
number_of_digits = len(digits_only)
# Determine country code and number formatting
if number_of_digits == 10:
local_number = digits_only[-10:]
formatted_number = "***-***-" + local_number[-4:]
return "+" + "*" * 0 + "-" + formatted_number
else:
country_code_length = number_of_digits - 10
country_code = '+' + '*' * country_code_length
local_number = digits_only[-10:]
# Mask the phone number as required
formatted_number = "***-***-" + local_number[-4:]
return country_code + '-' + formatted_number| Case | How to Handle |
|---|---|
| Null or empty input string | Return an empty string or throw an IllegalArgumentException since there is nothing to mask. |
| Email with less than 7 characters (e.g., 'a@b.c') | Return 'xxxxx@xxxxx.xxx' after the standard masking procedure, which implies masking as much as possible, even if insufficient chars exist. |
| Email with '+' or other special characters in username/domain | Treat '+' and other standard email characters normally during masking to 'xxxxx@xxxxx.xxx'. |
| Phone number with only 7 digits (no country code) | Mask as '***-***-xxxx' which assumes the local number format without a country code. |
| Phone number with non-numeric characters besides '+', '-', and spaces | Remove non-numeric characters before masking to ensure only digits are processed. |
| Phone number starting with '+0' | Treat it as country code 0 during the masking process of '+*-***-***-xxxx'. |
| Very long email address exceeding reasonable limits | Mask as 'xxxxx@xxxxx.xxx', scaling masking to the first 5 characters without performance issues. |
| Input string is neither a valid email nor a valid phone number | Return an empty string or throw an IllegalArgumentException indicating invalid format because there is no sensible masking possible. |