Problem

You are given a personal information string s, representing either an email address or a phone number. Return themasked personal information using the below rules.

Email address:

An email address is:

  • A name consisting of uppercase and lowercase English letters, followed by
  • The '@' symbol, followed by
  • The domain consisting of uppercase and lowercase English letters with a dot '.' somewhere in the middle (not the first or last character).

To mask an email:

  • The uppercase letters in the name and domain must be converted to lowercase letters.
  • The middle letters of the name (i.e., all but the first and last letters) must be replaced by 5 asterisks "*****".

Phone number:

A phone number is formatted as follows:

  • The phone number contains 10-13 digits.
  • The last 10 digits make up the local number.
  • The remaining 0-3 digits, in the beginning, make up the country code.
  • Separation characters from the set {'+', '-', '(', ')', ' '} separate the above digits in some way.

To mask a phone number:

  • Remove all separation characters.
  • The masked phone number should have the form:
  • "***-***-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.

Examples

Example 1

1
2
3
4
Input: s = "[email protected]"
Output: "l*****[email protected]"
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

1
2
3
4
5
Input: s = "[email protected]"
Output: "a*****[email protected]"
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

1
2
3
4
5
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.
  • If s is an email:
  • 8 <= s.length <= 40
  • s consists of uppercase and lowercase English letters and exactly one '@' symbol and '.' symbol.
    • If s is a phone number:
  • 10 <= s.length <= 20
  • s consists of digits, spaces, and the symbols '(', ')', '-', and '+'.

Solution

Method 1 – String Parsing and Formatting

Intuition

The problem is about masking either an email or a phone number. For emails, we mask the middle part of the name and lowercase everything. For phone numbers, we keep only digits, mask all but the last 4, and format according to the number of digits.

Approach

  1. If the string contains ‘@’, treat it as an email:
    • Lowercase the string.
    • Split into name and domain.
    • Mask the middle of the name with 5 asterisks, keep the first and last character.
  2. Otherwise, treat it as a phone number:
    • Remove all non-digit characters.
    • The last 10 digits are the local number; the rest are the country code.
    • Mask all but the last 4 digits, format as required.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
public:
    string maskPII(string s) {
        if (s.find('@') != string::npos) {
            string res;
            for (auto& c : s) res += tolower(c);
            int at = res.find('@');
            return res.substr(0,1) + "*****" + res.substr(at-1,1) + res.substr(at);
        } else {
            string digits;
            for (auto c : s) if (isdigit(c)) digits += c;
            string local = "***-***-" + digits.substr(digits.size()-4);
            if (digits.size() == 10) return local;
            string country = "+" + string(digits.size()-10, '*') + "-";
            return country + local;
        }
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
func maskPII(s string) string {
    if strings.Contains(s, "@") {
        s = strings.ToLower(s)
        at := strings.Index(s, "@")
        return s[:1] + "*****" + s[at-1:at] + s[at:]
    }
    digits := ""
    for _, c := range s {
        if c >= '0' && c <= '9' {
            digits += string(c)
        }
    }
    local := "***-***-" + digits[len(digits)-4:]
    if len(digits) == 10 {
        return local
    }
    country := "+" + strings.Repeat("*", len(digits)-10) + "-"
    return country + local
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    public String maskPII(String s) {
        if (s.contains("@")) {
            s = s.toLowerCase();
            int at = s.indexOf('@');
            return s.charAt(0) + "*****" + s.charAt(at-1) + s.substring(at);
        } else {
            StringBuilder digits = new StringBuilder();
            for (char c : s.toCharArray()) if (Character.isDigit(c)) digits.append(c);
            String local = "***-***-" + digits.substring(digits.length()-4);
            if (digits.length() == 10) return local;
            String country = "+" + "*".repeat(digits.length()-10) + "-";
            return country + local;
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    fun maskPII(s: String): String {
        if ("@" in s) {
            val t = s.lowercase()
            val at = t.indexOf('@')
            return t[0] + "*****" + t[at-1] + t.substring(at)
        } else {
            val digits = s.filter { it.isDigit() }
            val local = "***-***-" + digits.takeLast(4)
            return if (digits.length == 10) local else "+" + "*".repeat(digits.length-10) + "-" + local
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
    def maskPII(self, s: str) -> str:
        if '@' in s:
            s = s.lower()
            name, domain = s.split('@')
            return name[0] + '*****' + name[-1] + '@' + domain
        digits = [c for c in s if c.isdigit()]
        local = '***-***-' + ''.join(digits[-4:])
        if len(digits) == 10:
            return local
        return '+' + '*' * (len(digits)-10) + '-' + local
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
impl Solution {
    pub fn mask_pii(s: String) -> String {
        if s.contains('@') {
            let s = s.to_lowercase();
            let at = s.find('@').unwrap();
            let bytes = s.as_bytes();
            let mut res = String::new();
            res.push(bytes[0] as char);
            res.push_str("*****");
            res.push(bytes[at-1] as char);
            res.push_str(&s[at..]);
            res
        } else {
            let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
            let local = format!("***-***-{}", &digits[digits.len()-4..]);
            if digits.len() == 10 {
                return local;
            }
            let country = format!("+{}-", "*".repeat(digits.len()-10));
            format!("{}{}", country, local)
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    maskPII(s: string): string {
        if (s.includes('@')) {
            s = s.toLowerCase();
            const at = s.indexOf('@');
            return s[0] + '*****' + s[at-1] + s.slice(at);
        }
        const digits = Array.from(s).filter(c => c >= '0' && c <= '9').join('');
        const local = '***-***-' + digits.slice(-4);
        if (digits.length === 10) return local;
        return '+' + '*'.repeat(digits.length-10) + '-' + local;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of the input string, as we scan the string a constant number of times.
  • 🧺 Space complexity: O(n), for storing the processed string and digits.