#831

Masking Personal Information

specialist · 865 · lc medium +31 · verified · 54.2% accepted · 200 likes · top 46%

Description

Given a personal information string s that is either an email address or a phone number, return the masked version according to the following rules.

Email address:

An email address has:

- A name (uppercase and lowercase letters), followed by

- The '@' symbol, followed by

- A domain (letters with a '.' somewhere in the middle, not at the first or last position).

To mask an email:

- Convert all letters in the name and domain to lowercase.

- Replace every letter in the name except the first and last with five asterisks "*****".

Phone number:

A phone number contains 10-13 digits. The last 10 digits form the local number; any leading 0-3 digits form the country code. Separation characters {'+', '-', '(', ')', ' '} may appear.

To mask a phone number:

- Strip all separation characters.

- Format the result as:

- "***-***-XXXX" if there is no country code.

- "+*-***-***-XXXX" for a 1-digit country code.

- "+**-***-***-XXXX" for a 2-digit country code.

- "+***-***-***-XXXX" for a 3-digit country code.

- "XXXX" represents the last 4 digits of the local number.

Example 1:

Input: s = "LeetCode@LeetCode.com"
Output: "l*****e@leetcode.com"
Explanation: 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"
Explanation: 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"
Explanation: 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".

Code

1
2
3