algoadvance

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:

  1. 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”

  2. 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

  1. Email Characters: Does the email domain need to be case preserved?
    • No, the entire email should be lowercased.
  2. 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

  1. Identify Type of Data:
    • Check if the input contains an “@” symbol to determine if it is an email.
  2. 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 “*****”.
  3. 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

Overall, both scenarios operate in linear time relative to the length of the input string.

Try our interview co-pilot at AlgoAdvance.com