The "Masking Personal Information" problem requires you to write a function that takes a string s
representing either an email address or a phone number and returns the masked version of that personal information.
s
is an email address:
s
is a phone number:
***-***-XXXX
for local numbers, or +**-***-***-XXXX
etc. for numbers with a country code, where X
are the last 4 digits.The function must handle both formats, and return the correctly masked version based on the input.
The first step is to distinguish between an email and a phone number. Since emails always contain an @
, we can use this to decide how to process the input.
@
, lowercase everything, and mask the name part except for the first and last character.
The brute-force approach would be to check every possible masking, but since the requirements are clear and deterministic, we can directly apply the masking rules. This avoids unnecessary computations and makes for a concise solution.
@
, treat it as an email. Otherwise, treat it as a phone number.@
.@
, and domain.***-***-XXXX
+**-***-***-XXXX
(where **
is replaced by the appropriate number of asterisks for the country code length)This approach ensures that both email and phone number masking are handled efficiently and according to the problem's requirements.
Let's consider two examples:
"LeetCode@LeetCode.com"
"leetcode@leetcode.com"
"leetcode"
; Domain: "leetcode.com"
'l'
and 'e'
"l*****e@leetcode.com"
"86-(10)12345678"
"861012345678"
"86"
(first 2 digits), local number: "1012345678"
"+**-***-***-5678"
The function correctly identifies the type and applies the required masking.
O(N)
, where N
is the length of the input string. We scan the string once for digits or lowercasing.O(N)
, for storing the lowercased string and the digits (in the phone number case).The solution to the "Masking Personal Information" problem is straightforward due to the clear rules for emails and phone numbers. By first identifying the input type, then applying deterministic masking logic, we avoid brute-force solutions and ensure efficiency. The use of string manipulation and regular expressions (for digits) makes the solution both concise and effective.
class Solution:
def maskPII(self, s: str) -> str:
s = s.strip()
if '@' in s:
name, domain = s.split('@')
name = name.lower()
domain = domain.lower()
masked = name[0] + '*****' + name[-1] + '@' + domain
return masked
else:
digits = [c for c in s if c.isdigit()]
local = ''.join(digits[-10:])
country = digits[:-10]
masked_local = "***-***-" + local[-4:]
if not country:
return masked_local
else:
return "+" + "*" * len(country) + "-" + masked_local
class Solution {
public:
string maskPII(string s) {
if (s.find('@') != string::npos) {
// Email
transform(s.begin(), s.end(), s.begin(), ::tolower);
int at = s.find('@');
string name = s.substr(0, at);
string domain = s.substr(at);
string masked = string(1, name[0]) + "*****" + string(1, name.back()) + domain;
return masked;
} else {
// Phone
string digits;
for (char c : s) {
if (isdigit(c)) digits += c;
}
string local = digits.substr(digits.size() - 10);
string masked_local = "***-***-" + local.substr(6, 4);
if (digits.size() == 10) {
return masked_local;
} else {
string country = "+" + string(digits.size() - 10, '*') + "-";
return country + masked_local;
}
}
}
};
class Solution {
public String maskPII(String s) {
s = s.trim();
if (s.contains("@")) {
String[] parts = s.split("@");
String name = parts[0].toLowerCase();
String domain = parts[1].toLowerCase();
return name.charAt(0) + "*****" + name.charAt(name.length() - 1) + "@" + domain;
} else {
StringBuilder digits = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) digits.append(c);
}
String d = digits.toString();
String local = d.substring(d.length() - 10);
String maskedLocal = "***-***-" + local.substring(6);
if (d.length() == 10) {
return maskedLocal;
} else {
String country = "+" + "*".repeat(d.length() - 10) + "-";
return country + maskedLocal;
}
}
}
}
var maskPII = function(s) {
s = s.trim();
if (s.indexOf('@') !== -1) {
let [name, domain] = s.split('@');
name = name.toLowerCase();
domain = domain.toLowerCase();
return name[0] + '*****' + name[name.length - 1] + '@' + domain;
} else {
let digits = s.replace(/\D/g, '');
let local = digits.slice(-10);
let country = digits.slice(0, -10);
let maskedLocal = "***-***-" + local.slice(-4);
if (country.length === 0) {
return maskedLocal;
} else {
return "+" + "*".repeat(country.length) + "-" + maskedLocal;
}
}
};