Normalize and mask a personal identifier by applying email or phone-number formatting rules.
835. Masking Personal Information
gfgYou 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.
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:
*,***-***-XXXX for 10 digits,+*-***-***-XXXX for 11 digits,+**-***-***-XXXX for 12 digits,+***-***-***-XXXX for 13 digits.Return the masked string.
A single string s representing either an email address or a phone number.
Return a single string: the masked version of s.
s is a valid email address or a valid phone number.@.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
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.