Problem Description
Given a string that represents either an email address or a phone number, mask the personal information according to the following rules: For emails, convert all letters to lowercase and replace the middle characters of the name with five asterisks; for phone numbers, remove all separation characters, extract the local number (last 10 digits), determine the country code (if any), and then format the masked output accordingly.
Key Insights
- Check if the input is an email (contains '@') or a phone number.
- For emails:
- Convert the entire string to lowercase.
- Split the string into name and domain.
- Replace the middle characters of the name with "*****", keeping only the first and last characters.
- For phone numbers:
- Remove all non-digit characters.
- Identify the local number as the last 10 digits.
- The remaining digits (if any) represent the country code.
- Based on the length of the country code, construct the masked phone number accordingly.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the input string (processing each character once).
Space Complexity: O(n), due to the storage used for the output string and intermediate representations.
Solution
The solution first determines if the input string is an email or a phone number by checking for the presence of the '@' symbol. For an email, the entire string is converted to lowercase, and the name is masked by keeping its first and last letters while replacing the middle with five asterisks. For a phone number, all non-digit characters are removed to get a sequence of digits. The last 10 digits are taken as the local number, and any preceding digits are treated as the country code. Depending on the number of digits in the country code, a mask is prepended to the locally masked number (which is formatted as "--XXXX" where "XXXX" are the last 4 digits of the local number).