Skip to main content
Back to problems
Leetcode
Medium
Strings
Masking Personal Information

Normalize and mask a personal identifier by applying email or phone-number formatting rules.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.

835. Masking Personal Information

gfg
Problem Statement

Masking 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:

Email

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:
    • ***-***-XXXX for 10 digits,
    • +*-***-***-XXXX for 11 digits,
    • +**-***-***-XXXX for 12 digits,
    • +***-***-***-XXXX for 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

  • s is 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.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.