Masking Personal Information - Problem

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:
• A name consisting of uppercase and lowercase English letters, followed by
• The '@' symbol, followed by
• The domain consisting of uppercase and lowercase English letters with a dot '.' somewhere in the middle (not the first or last character).

To mask an email:
• The uppercase letters in the name and domain must be converted to lowercase letters.
• The middle letters of the name (i.e., all but the first and last letters) must be replaced by 5 asterisks "*****".

Phone number:
A phone number is formatted as follows:
• The phone number contains 10-13 digits.
• The last 10 digits make up the local number.
• The remaining 0-3 digits, in the beginning, make up the country code.
• Separation characters from the set {'+', '-', '(', ')', ' '} separate the above digits in some way.

To mask a phone number:
• Remove all separation characters.
• The masked phone number should have the form:
"***-***-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.

Input & Output

Example 1 — Email Masking
$ Input: s = "LeetCode@LeetCode.com"
Output: "l*****e@leetcode.com"
💡 Note: Email detected by '@' symbol. Convert to lowercase, mask middle of name 'LeetCode' → 'l*****e', keep domain 'leetcode.com'
Example 2 — Phone with Country Code
$ Input: s = "1(234)567-890"
Output: "***-***-7890"
💡 Note: Phone number with 10 digits total. Country code = 0 digits (no country code), local number is all 10 digits. Format: ***-***-XXXX where XXXX = 7890
Example 3 — Simple Phone
$ Input: s = "123-45-6789"
Output: "***-***-6789"
💡 Note: Phone number with exactly 10 digits. No country code (0 digits). Format: ***-***-XXXX where XXXX = 6789

Constraints

  • s is either a valid email or a valid phone number
  • If s is an email: 3 ≤ s.length ≤ 40
  • If s is a phone number: 10 ≤ s.length ≤ 20

Visualization

Tap to expand
Masking Personal Information INPUT Input String s: "LeetCode@LeetCode.com" Structure Analysis: LeetCode (name) @ LeetCode.com (domain) Type Detection: Contains '@' --> EMAIL Name Characters: L e e t C o d e Keep Replace with ***** Keep ALGORITHM STEPS 1 Detect Type Check for '@' symbol Found '@' --> Email 2 Split Email Parts name = "LeetCode" domain = "LeetCode.com" 3 Convert to Lowercase name --> "leetcode" domain --> "leetcode.com" 4 Mask Name first + "*****" + last "l" + "*****" + "e" Transformation: "LeetCode" (8 chars) becomes "l*****e" (7 chars) 5 Combine Result masked_name + "@" + domain FINAL RESULT Masked Email: "l*****e@leetcode.com" Result Breakdown: l*****e (masked) @ leetcode.com (lowercase) Verification: OK First char kept: 'l' OK Last char kept: 'e' OK 5 asterisks in middle OK All lowercase Phone Example: "+1(234)567-890" --> "+*-***-***-7890" Key Insight: The problem requires different masking strategies based on input type. For emails: detect '@', convert to lowercase, and mask middle characters of name with exactly 5 asterisks. For phones: extract digits, determine country code length, and format with appropriate number of asterisks. TutorialsPoint - Masking Personal Information | String Operations Approach
Asked in
Google 15 Amazon 12 Facebook 8
35.4K 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