LeetCode Problem Workspace
Masking Personal Information
This problem requires masking sensitive parts of emails and phone numbers using a precise string-driven strategy efficiently.
1
Topics
5
Code langs
3
Related
Practice Focus
Medium · String-driven solution strategy
Answer-first summary
This problem requires masking sensitive parts of emails and phone numbers using a precise string-driven strategy efficiently.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for String-driven solution strategy
Start by quickly identifying whether the input is an email or phone number. For emails, convert to lowercase and replace the middle letters of the name with five asterisks while preserving the first and last characters. For phone numbers, strip non-digit characters, determine the country code length, and mask all but the last four digits. This approach ensures a consistent, secure output while following a string manipulation pattern directly tied to the problem.
Problem Statement
You are given a string s that contains personal information, either an email address or a phone number. Your task is to return a masked version of this information following specific rules to protect privacy.
For emails, convert all letters to lowercase and replace all characters between the first and last letter of the name with five asterisks. For phone numbers, remove all non-digit characters, identify the local and country code, and mask every digit except the last four with asterisks, preserving formatting as needed.
Examples
Example 1
Input: s = "LeetCode@LeetCode.com"
Output: "l*e@leetcode.com"
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"
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"
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.
- If s is an email:
8 <= s.length <= 40 s consists of uppercase and lowercase English letters and exactly one '@' symbol and '.' symbol.
- 8 <= s.length <= 40
- s consists of uppercase and lowercase English letters and exactly one '@' symbol and '.' symbol.
- If s is a phone number:
10 <= s.length <= 20 s consists of digits, spaces, and the symbols '(', ')', '-', and '+'.
- 10 <= s.length <= 20
- s consists of digits, spaces, and the symbols '(', ')', '-', and '+'.
Solution Approach
Detect Input Type
Check if s contains '@' to classify it as an email; otherwise, treat it as a phone number. This initial string inspection prevents misclassification and directs the correct masking logic.
Email Masking Strategy
Split the email into name and domain. Convert both to lowercase. Replace the middle characters of the name with five asterisks, keeping only the first and last character visible. Concatenate the masked name with the domain to produce the final output.
Phone Number Masking Strategy
Remove all characters except digits. Determine the country code length by subtracting 10 from the total digits. Prepend asterisks for the country code if present. Mask all local digits except the last four, maintaining the standard formatting with dashes.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n) because each character in s is processed once for classification and masking. Space complexity is O(n) for storing intermediate results and the final masked string.
What Interviewers Usually Probe
- Look for immediate input classification to avoid processing errors.
- Check string lowercasing and proper insertion of asterisks for emails.
- Verify correct digit masking and country code handling for phone numbers.
Common Pitfalls or Variants
Common pitfalls
- Incorrectly counting characters in short email names and skipping the required five asterisks.
- Failing to remove non-digit characters before phone number masking.
- Mixing up local digits and country code leading to an incorrect masked output.
Follow-up variants
- Masking emails with multiple subdomains and ensuring the domain remains intact.
- Handling international phone numbers with varying country code lengths.
- Masking hybrid strings that mix email and phone formatting and require conditional logic.
FAQ
What is the main pattern used in Masking Personal Information?
The primary pattern is string-driven manipulation: detect type, transform characters, and insert asterisks according to email or phone rules.
How many characters are replaced in email names?
Always replace the middle section of the email name with exactly five asterisks, regardless of the name length.
Do I need to preserve the original case of letters?
No, all email characters should be converted to lowercase to standardize the masked output.
How are phone numbers with country codes masked?
Mask all digits except the last four of the local number, prepend asterisks for any country code, and maintain formatting with dashes.
Can this approach handle both emails and phone numbers in a single function?
Yes, by first classifying the input using the presence of '@', then applying the respective masking strategy for each type.
Solution
Solution 1: Simulation
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.
class Solution:
def maskPII(self, s: str) -> str:
if s[0].isalpha():
s = s.lower()
return s[0] + '*****' + s[s.find('@') - 1 :]
s = ''.join(c for c in s if c.isdigit())
cnt = len(s) - 10
suf = '***-***-' + s[-4:]
return suf if cnt == 0 else f'+{"*" * cnt}-{suf}'Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
String-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Medium
Stay on this level to stabilize interview delivery.
arrow_forward