Advertisement

Masking Personal Information - LeetCode 831 Solution

Masking Personal Information - Complete Solution Guide

Masking Personal Information is LeetCode problem 831, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem requires masking personal information, specifically email addresses and phone numbers, according to defined rules. For email addresses, the username and domain are converted to lowercase, the middle characters of the username are replaced with asterisks. For phone numbers, separation characters are removed, and the number is masked based on the number of digits, indicating the country code. The last four digits of the local number are always revealed.

Solution Approach

The solution determines whether the input string is an email or phone number based on the presence of the '@' character. For emails, it converts the string to lowercase, splits it into username and domain, masks the username, and concatenates the parts. For phone numbers, it removes non-digit characters, determines the country code length, and masks the number accordingly.

Step-by-Step Algorithm

  1. Step 1: Check if the input string `s` contains the character '@'. If it does, process it as an email; otherwise, process it as a phone number.
  2. Step 2: If it's an email, convert `s` to lowercase.
  3. Step 3: Split the email into `name` and `domain` using the '@' character as the delimiter.
  4. Step 4: Mask the `name` by taking the first and last characters and replacing the characters in between with five asterisks: `name[0] + '*****' + name[-1]`.
  5. Step 5: Concatenate the masked `name`, '@', and the `domain` to form the masked email.
  6. Step 6: If it's a phone number, remove all non-digit characters from `s`.
  7. Step 7: Determine the number of digits in the cleaned phone number.
  8. Step 8: Extract the last four digits of the phone number.
  9. Step 9: Based on the number of digits, construct the masked phone number string. If the number of digits is 10, the masked string is `***-***-{last four digits}`. If the number of digits is greater than 10, calculate the country code length and mask it with asterisks accordingly e.g., `+{country code mask}-***-***-{last four digits}`.

Key Insights

  • Insight 1: The problem can be solved by straightforward string manipulation and conditional logic, separating the email and phone number processing based on the presence of the '@' character.
  • Insight 2: Regular expressions can be used to remove non-digit characters from phone numbers.
  • Insight 3: The country code length determines the specific masking pattern for phone numbers; careful consideration is needed to handle different country code lengths.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: String.

Companies

Asked at: X.