The Masking Personal Information problem asks you to write a function that takes a string representing either an Email Address or a Phone Number. Your task is to mask (obfuscate) the string according to very specific formatting rules. For emails, you must lowercase everything, keep the first and last letters of the name, and replace the middle with *****. For phone numbers, you must strip all formatting, keep the last 4 digits visible, and mask the rest with ***-***-, including any international country codes.
This is a pure string manipulation and parsing problem. Companies ask it because sanitizing PII (Personally Identifiable Information) is a daily reality for backend engineers. It evaluates your attention to detail, your ability to handle multiple conditional formatting rules, and your familiarity with string libraries, regex, and character replacement.
The problem does not require complex algorithms, but rather a Parsing and Conditional Formatting pattern.
'@', it's an email. Otherwise, it's a phone number.'@'. Convert the name part to lowercase, extract the first character and the last character, and concatenate them with ***** in between. Append the @ and the lowercase domain.+, -, (, ), ). Count the remaining digits. Take the last 4 digits. Prepend the local mask ***-***-. If the digit count is greater than 10, prepend the country code mask (e.g., +*- or +**-).Email: "LeetCode@LeetCode.com"
@. It's an email."LeetCode", Domain "LeetCode.com"."leetcode". First char 'l', last char 'e'."l*****e"."leetcode.com"."l*****e@leetcode.com".Phone: "1(234)567-890"
@. It's a phone."1234567890"."7890"."***-***-7890".A major pitfall is failing to properly strip all the distinct non-digit characters from the phone number. Using regex like s.replaceAll("[^0-9]", "") is the safest way to extract just the digits. Another common mistake is hardcoding index substrings for the phone number without counting the total digits first, causing out-of-bounds exceptions when international codes are present.
For the Masking Personal Information coding problem, utilize your language's built-in string methods to save time. In Python, str.lower() and list comprehensions are great. In Java, StringBuilder and replaceAll("\\D", "") will handle the phone number cleanup cleanly. Always handle the structural split (Email vs Phone) at the very top of your function to keep the logic branches completely isolated.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Validate IP Address | Medium | Solve | |
| Zigzag Conversion | Medium | Solve | |
| Apply Discount to Prices | Medium | Solve | |
| Count and Say | Medium | Solve | |
| Length of the Longest Alphabetical Continuous Substring | Medium | Solve |