Masking Personal Information

Medium
1209.

Masking Personal Information

Masking Personal Information is a medium coding problem on strings, asked in Amazon, Microsoft and Twitter interviews.

The string s is either an email address or a phone number. Return it masked by the rules below.

Email address — name@domain, where name is at least two English letters and domain is English letters with a single . somewhere in the middle. To mask it, convert every letter to lowercase and replace all letters of name except its first and last with exactly five asterisks *****.

Phone number — 10 to 13 digits, possibly mixed with the separator characters +, -, (, ) and space. The last 10 digits are the local number; any digits before them (0 to 3) are the country code. To mask it, drop every separator and write the local number as ***-***-XXXX, where XXXX is its last four digits. With a country code of k digits, prefix +, then k asterisks, then -: for example +**-***-***-XXXX.

Example 1

Input
s = "CodeKairo@Mail.Com"
Output
"c*****o@mail.com"

Example 2

Input
s = "+91 (987) 654-3210"
Output
"+**-***-***-3210"
Explanation
12 digits: a 2-digit country code and the local number.

Example 3

Input
s = "(555) 010-9999"
Output
"***-***-9999"

Constraints

  • s is a valid email address or phone number
  • For an email: 8 <= s.length <= 40
  • For a phone number: 10 <= s.length <= 20
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} s
 * @return {string}
 */
var maskPII = function(s) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Masking Personal Information — Problem & Solution

Masking Personal Information solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem on strings.

Masking Personal Information is a medium coding problem on strings, asked in Amazon, Microsoft and Twitter interviews.

  • Difficulty: Medium
  • Topics: Strings
  • Asked at: Amazon, Microsoft, Twitter
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

The string s is either an email address or a phone number. Return it masked by the rules below.

Email address — name@domain, where name is at least two English letters and domain is English letters with a single . somewhere in the middle. To mask it, convert every letter to lowercase and replace all letters of name except its first and last with exactly five asterisks *****.

Phone number — 10 to 13 digits, possibly mixed with the separator characters +, -, (, ) and space. The last 10 digits are the local number; any digits before them (0 to 3) are the country code. To mask it, drop every separator and write the local number as ***-***-XXXX, where XXXX is its last four digits. With a country code of k digits, prefix +, then k asterisks, then -: for example +**-***-***-XXXX.

Example 1

Input: s = "CodeKairo@Mail.Com"
Output: "c*****o@mail.com"

Example 2

Input: s = "+91 (987) 654-3210"
Output: "+**-***-***-3210"
Explanation: 12 digits: a 2-digit country code and the local number.

Example 3

Input: s = "(555) 010-9999"
Output: "***-***-9999"

Constraints

  • s is a valid email address or phone number
  • For an email: 8 <= s.length <= 40
  • For a phone number: 10 <= s.length <= 20

Hints for Masking Personal Information

  1. Hint 1

    Decide which kind of input it is by looking for @.

  2. Hint 2

    For an email, lowercase the whole string and keep only the first and last letters of the part before @.

  3. Hint 3

    For a phone number, keep only the digits; the masked form depends only on how many there are and on the last four.

How to solve Masking Personal Information

The two formats are told apart by the @; after that each mask is fixed text plus a few kept characters.

Approach

  1. If s contains @ at index at: lowercase s and return s[0] + "*****" + s[at − 1] + s[at…] (the last letter of the name, then @ and the domain).
  2. Otherwise collect the digits of s in order. Let local = "***-***-" + last four digits.
  3. If there are exactly 10 digits, return local; otherwise return "+" + "*" × (digits − 10) + "-" + local.

Why it works

The email mask always shows five asterisks no matter how long the name is, so only the first and last name letters and the lowercased domain survive. For phones the separators carry no information, and the masked shape depends only on the number of digits (which fixes the country-code length) and the last four digits, which are the only ones shown.

Complexity

  • Time — O(n)
  • Space — O(n)

Pitfalls

  • The number of asterisks in an email mask is always five, not the number of hidden letters.
  • The domain must be lowercased too, not just the name.
  • A 10-digit phone number gets no + prefix and no leading dash.

Reference solution

Python

def maskPII(s: str) -> str:
    at = s.find("@")
    if at >= 0:
        low = s.lower()
        return low[0] + "*****" + low[at - 1:]
    digits = [c for c in s if c.isdigit()]
    local = "***-***-" + "".join(digits[-4:])
    cc = len(digits) - 10
    return local if cc == 0 else "+" + "*" * cc + "-" + local

Java

public static String maskPII(String s) {
    int at = s.indexOf('@');
    if (at >= 0) {
        String low = s.toLowerCase();
        return low.charAt(0) + "*****" + low.substring(at - 1);
    }
    StringBuilder digits = new StringBuilder();
    for (char c : s.toCharArray()) if (c >= '0' && c <= '9') digits.append(c);
    String local = "***-***-" + digits.substring(digits.length() - 4);
    if (digits.length() == 10) return local;
    StringBuilder sb = new StringBuilder("+");
    for (int k = 10; k < digits.length(); k++) sb.append('*');
    return sb.append('-').append(local).toString();
}

C++

string maskPII(string s) {
    size_t at = s.find('@');
    if (at != string::npos) {
        for (char& c : s) if (c >= 'A' && c <= 'Z') c = (char)(c - 'A' + 'a');
        return string(1, s[0]) + "*****" + s.substr(at - 1);
    }
    string digits;
    for (char c : s) if (c >= '0' && c <= '9') digits.push_back(c);
    string local = "***-***-" + digits.substr(digits.size() - 4);
    if (digits.size() == 10) return local;
    return "+" + string(digits.size() - 10, '*') + "-" + local;
}

JavaScript

var maskPII = function(s) {
    var at = s.indexOf("@");
    if (at >= 0) {
        var low = s.toLowerCase();
        return low[0] + "*****" + low.substring(at - 1);
    }
    var digits = "";
    for (var i = 0; i < s.length; i++) {
        var c = s.charCodeAt(i);
        if (c >= 48 && c <= 57) digits += s[i];
    }
    var local = "***-***-" + digits.substring(digits.length - 4);
    if (digits.length === 10) return local;
    var stars = "";
    for (var k = 10; k < digits.length; k++) stars += "*";
    return "+" + stars + "-" + local;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings