Masking Personal Information - Problem
Masking Personal Information is a common privacy protection technique used in data processing systems. You're given a personal information string s that represents either an email address or a phone number. Your task is to return the properly masked version following specific rules.

๐Ÿ“ง Email Masking Rules:
โ€ข Convert all letters to lowercase
โ€ข Keep only the first and last letter of the name part (before @)
โ€ข Replace middle letters with exactly 5 asterisks "*****"
โ€ข Example: "John@Example.com" โ†’ "j*****n@example.com"

๐Ÿ“ฑ Phone Number Masking Rules:
โ€ข Remove all separators: +, -, (, ), space
โ€ข Last 10 digits are the local number, remaining 0-3 digits are country code
โ€ข Format based on country code length:
  - 0 digits: "***-***-XXXX"
  - 1 digit: "+*-***-***-XXXX"
  - 2 digits: "+**-***-***-XXXX"
  - 3 digits: "+***-***-***-XXXX"
โ€ข XXXX represents the last 4 digits of the local number

Input & Output

example_1.py โ€” Email Masking
$ Input: s = "LeetCode@LeetCode.com"
โ€บ Output: "l*****e@leetcode.com"
๐Ÿ’ก Note: The email name 'LeetCode' becomes 'l*****e' (first + 5 asterisks + last), and everything is converted to lowercase
example_2.py โ€” Domestic Phone
$ Input: s = "1(234)567-890"
โ€บ Output: "***-***-7890"
๐Ÿ’ก Note: After removing separators we get '1234567890' (10 digits), so no country code. Format is '***-***-XXXX' where XXXX is '7890'
example_3.py โ€” International Phone
$ Input: s = "86-(10)12345678"
โ€บ Output: "+**-***-***-5678"
๐Ÿ’ก Note: After removing separators we get '861012345678' (12 digits), so 2-digit country code '86'. Format is '+**-***-***-XXXX'

Constraints

  • 1 โ‰ค s.length โ‰ค 40
  • s is either a valid email or a phone number
  • If s is an email: name and domain contain only letters, domain has exactly one '.' separator
  • If s is a phone number: it contains 10-13 digits total

Visualization

Tap to expand
Privacy Protection ScannerInput Documentjohn@example.com+1(800)555-0123Pattern ScannerDetect '@' โ†’ EmailAll digits โ†’ PhoneMasked Outputj*****n@example.com+*-***-***-0123Masking Protocols๐Ÿ“ง Email Protocol1. Convert to lowercase2. Find '@' separator3. Extract name part4. Keep first + last char5. Replace middle with '*****'6. Combine with domain๐Ÿ“ฑ Phone Protocol1. Remove all separators2. Count total digits3. Identify country code length4. Apply format template5. Show last 4 digits only6. Mask everything else
Understanding the Visualization
1
Document Scanning
Scanner detects '@' symbol to classify as email vs phone number
2
Email Protocol
Apply email masking: convert to lowercase, keep first/last of name, mask middle with 5 stars
3
Phone Protocol
Apply phone masking: extract digits, determine country code length, format accordingly
4
Output Generation
Return properly masked result following privacy standards
Key Takeaway
๐ŸŽฏ Key Insight: Pattern recognition allows instant classification - emails have '@' symbols, phones are pure digits after separator removal, enabling efficient single-pass processing with appropriate masking protocols.
Asked in
Facebook 12 Amazon 8 Google 6 Microsoft 4
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen