Leetcode 831: Masking Personal Information

grid47
grid47
Exploring patterns and algorithms
Aug 15, 2024 6 min read

You are given a string representing either an email address or a phone number. The task is to mask the personal information by following specific rules. For email addresses, the name and domain should be in lowercase, and the middle letters of the name should be replaced with asterisks. For phone numbers, any non-digit characters should be removed, and the number should be masked with asterisks based on the country code and local number.
Problem
Approach
Steps
Complexity
Input: The input is a string containing either a valid email address or a valid phone number.
Example: Input: s = 'JaneDoe@domain.com'
Constraints:
• If the input is an email, the length will be between 8 and 40 characters.
• If the input is a phone number, the length will be between 10 and 20 characters.
Output: Return the masked version of the input string, either an email address or a phone number, according to the specified rules.
Example: Output: 'j*****e@domain.com'
Constraints:
• The output must follow the masking rules provided for emails and phone numbers.
Goal: The goal is to mask the personal information string based on whether it is an email or a phone number, following the specified rules for each case.
Steps:
• Step 1: Check if the string contains '@', which indicates it is an email address.
• Step 2: For emails, convert all characters to lowercase and replace the middle part of the name with asterisks.
• Step 3: If it is a phone number, remove all non-digit characters and mask the number based on the number of digits in the country code.
Goal: Ensure the string follows the format of either a valid email address or phone number.
Steps:
• The email address contains exactly one '@' and one '.' symbol.
• The phone number consists of digits, spaces, and specific symbols like '-', '(', ')', and '+'.
Assumptions:
• The input string is always a valid email address or a phone number.
Input: Input: s = 'JohnSmith@Example.com'
Explanation: This is an email address. We convert the entire string to lowercase and replace the middle part of the name with asterisks: 'j*****h@example.com'.

Input: Input: s = '1(234)567-890'
Explanation: This is a phone number. After removing the non-digit characters, the number becomes '1234567890'. As it has no country code, it is masked as '***-***-7890'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus