Watch 8 video solutions for Masking Personal Information, a medium level problem involving String. This walkthrough by ExtremeProgrammer has 309 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a personal information string s, representing either an email address or a phone number. Return the masked personal information using the below rules.
Email address:
An email address is:
'@' symbol, followed by'.' somewhere in the middle (not the first or last character).To mask an email:
"*****".Phone number:
A phone number is formatted as follows:
{'+', '-', '(', ')', ' '} separate the above digits in some way.To mask a phone number:
"***-***-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.
Example 1:
Input: s = "LeetCode@LeetCode.com" Output: "l*****e@leetcode.com" 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:
Input: s = "AB@qq.com" Output: "a*****b@qq.com" 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:
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.s is an email:
8 <= s.length <= 40s consists of uppercase and lowercase English letters and exactly one '@' symbol and '.' symbol.s is a phone number:
10 <= s.length <= 20s consists of digits, spaces, and the symbols '(', ')', '-', and '+'.Problem Overview: You receive a string that represents either an email address or a phone number. The task is to mask sensitive parts of the string while preserving a few characters so the result still identifies the contact. Email masking keeps the first and last character of the name while replacing the middle with asterisks. Phone masking hides all digits except the last four and formats the result depending on whether a country code exists.
Approach 1: Email and Phone Number Detection and Masking (O(n) time, O(n) space)
The input type determines the masking rule. Scan the string once to check for the '@' character. If it exists, treat the input as an email; otherwise treat it as a phone number. For emails, convert everything to lowercase, keep the first and last character of the local name, and insert five asterisks between them before appending the domain. For phone numbers, iterate through the string and extract only digits, ignoring characters like '(', ')', '+', spaces, or dashes. Keep the last four digits visible and replace the rest with *. If more than ten digits exist, prepend the correct country code mask using +***- style formatting. This approach relies purely on string manipulation and a single pass through the input.
The key insight is separating detection from transformation. Once you know the input type, the masking rule becomes deterministic. You perform simple operations such as substring extraction, digit filtering, and concatenation. Time complexity is O(n) because each character is processed at most once, and space complexity is O(n) for constructing the masked output.
Approach 2: Regular Expression for Detecting Number of Digits (O(n) time, O(n) space)
This variation uses regular expressions to simplify parsing. Instead of manually iterating to filter digits, apply a regex pattern like \d to extract all digits from the input. The resulting list directly reveals how many digits the phone number contains, which determines the country code length. For emails, a simple split around '@' combined with lowercase conversion produces the masked format quickly.
Regex makes the implementation shorter and easier to read in languages that support strong pattern libraries. Digit extraction and counting happen in one step, avoiding manual conditional checks. Complexity remains O(n) time because the regex engine scans the string once, and O(n) space for storing extracted characters.
Recommended for interviews: The direct detection and masking approach is what interviewers usually expect. It shows that you can handle conditional parsing, apply exact formatting rules, and reason through edge cases such as international phone numbers. Regex solutions are concise and elegant but sometimes hide the logic interviewers want to see. Demonstrating the explicit string processing steps proves you understand how the masking rules are implemented.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Email and Phone Detection with Manual Parsing | O(n) | O(n) | Best for interviews and most implementations where explicit string processing is expected |
| Regex-Based Digit Extraction | O(n) | O(n) | Useful when regex libraries are available and concise parsing is preferred |