LeetCodee

831. Masking Personal Information

Jump to Solution: Python Java C++ JavaScript C#

Problem Description

You are given a personal information string s, which may be an email address or a phone number.

Return the masked personal information using the following rules:

Email address:

  • An email address is a string of the form "name@domain.com".
  • The name must be converted to lowercase with its first and last letters remaining, and all letters in between replaced by 5 asterisks (*).
  • For example, "Alice@leetcode.com" becomes "a*****e@leetcode.com".

Phone number:

  • A phone number is a string of digits that may contain '+', '-', and '(' and ')'.
  • Remove all non-digit characters and mask the last 10 digits as "***-***-XXXX".
  • If the number has more than 10 digits, prefix the masked digits with "+-".
  • For example, "1(234)567-890" becomes "***-***-8901" and "+111 123 456 789" becomes "+111-***-***-7890".

Examples:

Example 1:

Input: s = "LeetCode@LeetCode.com"
Output: "l*****e@leetcode.com"
Explanation: s is an email address.

Example 2:

Input: s = "AB@qq.com"
Output: "a*b@qq.com"
Explanation: s is an email address. Note that even though "AB" is in uppercase, we still return a lowercase string.

Example 3:

Input: s = "1(234)567-890"
Output: "***-***-8901"
Explanation: s is a phone number.

Constraints:

  • s is either a valid email or a phone number.
  • If s is an email: 8 ≤ s.length ≤ 40 and s consists of uppercase and lowercase English letters, digits, and '@', '.', and '-'.
  • If s is a phone number: 10 ≤ s.length ≤ 20 and s consists of digits, spaces, and the characters '(', ')', '+', and '-'.

Python Solution

class Solution:
    def maskPII(self, s: str) -> str:
        if '@' in s:
            # Email
            s = s.lower()
            name, domain = s.split('@')
            return f"{name[0]}*****{name[-1]}@{domain}"
        else:
            # Phone number
            digits = ''.join(c for c in s if c.isdigit())
            if len(digits) == 10:
                return f"***-***-{digits[-4:]}"
            else:
                return f"+{'*' * (len(digits)-10)}-***-***-{digits[-4:]}"

Implementation Notes:

  • Handles both email and phone number cases
  • Uses string manipulation techniques
  • Time complexity: O(n)
  • Space complexity: O(n)

Java Solution

class Solution {
    public String maskPII(String s) {
        if (s.contains("@")) {
            // Email
            s = s.toLowerCase();
            int atIndex = s.indexOf('@');
            return s.charAt(0) + "*****" + s.charAt(atIndex - 1) + s.substring(atIndex);
        } else {
            // Phone number
            String digits = s.replaceAll("\\D", "");
            if (digits.length() == 10) {
                return "***-***-" + digits.substring(digits.length() - 4);
            } else {
                return "+" + "*".repeat(digits.length() - 10) + "-***-***-" + 
                       digits.substring(digits.length() - 4);
            }
        }
    }
}

C++ Solution

class Solution {
public:
    string maskPII(string s) {
        if (s.find('@') != string::npos) {
            // Email
            transform(s.begin(), s.end(), s.begin(), ::tolower);
            int atIndex = s.find('@');
            return s[0] + string("*****") + s[atIndex - 1] + s.substr(atIndex);
        } else {
            // Phone number
            string digits;
            for (char c : s) {
                if (isdigit(c)) {
                    digits += c;
                }
            }
            if (digits.length() == 10) {
                return "***-***-" + digits.substr(digits.length() - 4);
            } else {
                return "+" + string(digits.length() - 10, '*') + "-***-***-" + 
                       digits.substr(digits.length() - 4);
            }
        }
    }
};

JavaScript Solution

/**
 * @param {string} s
 * @return {string}
 */
var maskPII = function(s) {
    if (s.includes('@')) {
        // Email
        s = s.toLowerCase();
        const [name, domain] = s.split('@');
        return `${name[0]}*****${name[name.length-1]}@${domain}`;
    } else {
        // Phone number
        const digits = s.replace(/\D/g, '');
        if (digits.length === 10) {
            return `***-***-${digits.slice(-4)}`;
        } else {
            return `+${'*'.repeat(digits.length-10)}-***-***-${digits.slice(-4)}`;
        }
    }
};

C# Solution

public class Solution {
    public string MaskPII(string s) {
        if (s.Contains('@')) {
            // Email
            s = s.ToLower();
            int atIndex = s.IndexOf('@');
            return $"{s[0]}*****{s[atIndex - 1]}{s.Substring(atIndex)}";
        } else {
            // Phone number
            string digits = new string(s.Where(char.IsDigit).ToArray());
            if (digits.Length == 10) {
                return $"***-***-{digits.Substring(digits.Length - 4)}";
            } else {
                return $"+{new string('*', digits.Length - 10)}-***-***-{digits.Substring(digits.Length - 4)}";
            }
        }
    }
}

Implementation Notes:

  • Uses string manipulation and LINQ
  • Handles different formats efficiently
  • Time complexity: O(n)
  • Space complexity: O(n)