We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Masking Personal Information

Number: 858

Difficulty: Medium

Paid? No

Companies: Salesforce, X


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


Code Solutions

# Python solution for masking personal information

def maskPII(s: str) -> str:
    # Check if the input is an email address by the presence of '@'
    if "@" in s:
        # Convert the entire string to lowercase for consistency
        s = s.lower()
        # Split the email into name and domain parts
        name, domain = s.split("@")
        # Mask the name: first character + 5 asterisks + last character
        return name[0] + "*****" + name[-1] + "@" + domain
    else:
        # Process as a phone number - remove all non-digit characters
        digits = "".join(char for char in s if char.isdigit())
        # Local number comprises the last 10 digits
        local = digits[-10:]
        # Determine the length of the country code (remaining digits)
        countryCodeLength = len(digits) - 10
        maskedLocal = "***-***-" + local[-4:]
        # If there is no country code, return the local masked number directly
        if countryCodeLength == 0:
            return maskedLocal
        # Otherwise, add a country code mask based on its length
        countryMask = "+" + "*" * countryCodeLength + "-"
        return countryMask + maskedLocal

# Example usage:
print(maskPII("LeetCode@LeetCode.com"))  # Output: l*****e@leetcode.com
print(maskPII("1(234)567-890"))          # Output: ***-***-7890
← Back to All Questions