Normalize and mask a personal identifier by applying email or phone-number formatting rules.
835. Masking Personal Information
gfgMasking Personal Information
You are given a string representing either an email address or a phone number. Return a masked version of the information according to the following rules:
An email address contains exactly one @. Convert it to lowercase and mask the local name so that only its first and last characters remain visible:
first*****last@domain
where the local name is replaced by exactly five * characters.
Phone Number
A phone number contains only digits and may also include spaces, dashes, parentheses, or a leading + sign. Keep only the digits. Then mask the number so that:
- the last 4 digits remain visible,
- the country code, if present, is hidden using
*, - the final format is:
***-***-XXXXfor 10 digits,+*-***-***-XXXXfor 11 digits,+**-***-***-XXXXfor 12 digits,+***-***-***-XXXXfor 13 digits.
Return the masked string.
Notes
- For emails, output must be all lowercase.
- For phone numbers, ignore all non-digit characters when extracting digits.
- The input is guaranteed to be a valid email address or a valid phone number in one of the supported formats.
Input Format
A single string s representing either an email address or a phone number.
Output Format
Return a single string: the masked version of s.
Constraints
sis a valid email address or a valid phone number.- Email addresses contain exactly one
@. - Phone numbers contain between 10 and 13 digits after removing formatting characters.
- Use only the masking rules described above.
Example 1
Input
LeetCode@LeetCode.com
Output
l*****e@leetcode.com
Explanation
This is an email. Lowercase it, keep the first and last characters of the local name, and replace the rest with five asterisks.
Example 2
Input
(86)-10-12345678
Output
+**-***-***-5678
Explanation
This is a phone number with 12 digits. Keep the last four digits and hide the country code with two asterisks.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.