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.
This approach involves first detecting whether the input is an email address or a phone number. For emails, the middle characters of the name part should be replaced with asterisks, and then both the name and domain should be converted to lowercase. For phone numbers, we remove non-numeric characters and replace the digits with the correct masking format based on the length.
The function first checks if the input has an '@' character. If it does, it's considered an email and is split into name and domain parts. The name is then masked with asterisks by using the first and last letter with '*****' in between, combined with the lowercase domain.
For phone numbers, all digits are extracted, and the function constructs the masked version based on the number of digits using string concatenations.
Python
JavaScript
Time complexity is O(n), where n is the length of the input string. Space complexity is O(1) as we are not using extra space relative to input size beyond required variables.
In this approach, we utilize regular expressions to identify phone numbers by counting the digits in the input. Once identified, we process and mask the input accordingly. For email, the masking continues as described previously.
This C++ solution first checks whether the input contains '@'. If yes, it treats it as an email, converts everything to lowercase, and masks the name part. Otherwise, it processes the input string to extract digits using loops and constructs the masked phone number accordingly.
Time complexity is O(n), the complexity is driven by the initial parsing of the string. Space complexity is O(n) due to storage of the digits string.
According to the problem description, we can first determine whether the string s is an email or a phone number, and then handle it accordingly.
The time complexity is O(n), and the space complexity is O(n), where n is the length of the string s.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Email and Phone Number Detection and Masking | Time complexity is O(n), where n is the length of the input string. Space complexity is O(1) as we are not using extra space relative to input size beyond required variables. |
| Regular Expression for Detecting Number of Digits | Time complexity is O(n), the complexity is driven by the initial parsing of the string. Space complexity is O(n) due to storage of the digits string. |
| Simulation | — |
| 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 |
Leetcode 831: Masking Personal Information • ExtremeProgrammer • 309 views views
Watch 7 more video solutions →Practice Masking Personal Information with our built-in code editor and test cases.
Practice on FleetCode