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:
'@' symbol, followed by'.' somewhere in the middle (not the first or last character).To mask an email:
"*****".Phone number:
A phone number is formatted as follows:
{'+', '-', '(', ')', ' '} separate the above digits in some way.To mask a phone number:
"***-***-XXXX" if the country code has 0 digits."+*-***-***-XXXX" if the country code has 1 digit."+**-***-***-XXXX" if the country code has 2 digits."+***-***-***-XXXX" if the country code has 3 digits."XXXX" is 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".
Constraints:
s is either a valid email or a phone number.s is an email:
8 <= s.length <= 40s consists of uppercase and lowercase English letters and exactly one '@' symbol and '.' symbol.s is a phone number:
10 <= s.length <= 20s consists of digits, spaces, and the symbols '(', ')', '-', and '+'.This approach involves first detecting whether the input is an email address or a phone number. For emails, the middle characters of the name part should be replaced with asterisks, and then both the name and domain should be converted to lowercase. For phone numbers, we remove non-numeric characters and replace the digits with the correct masking format based on the length.
The function first checks if the input has an '@' character. If it does, it's considered an email and is split into name and domain parts. The name is then masked with asterisks by using the first and last letter with '*****' in between, combined with the lowercase domain.
For phone numbers, all digits are extracted, and the function constructs the masked version based on the number of digits using string concatenations.
JavaScript
Time complexity is O(n), where n is the length of the input string. Space complexity is O(1) as we are not using extra space relative to input size beyond required variables.
In this approach, we utilize regular expressions to identify phone numbers by counting the digits in the input. Once identified, we process and mask the input accordingly. For email, the masking continues as described previously.
This C++ solution first checks whether the input contains '@'. If yes, it treats it as an email, converts everything to lowercase, and masks the name part. Otherwise, it processes the input string to extract digits using loops and constructs the masked phone number accordingly.
C
Time complexity is O(n), the complexity is driven by the initial parsing of the string. Space complexity is O(n) due to storage of the digits string.
| Approach | Complexity |
|---|---|
| Email and Phone Number Detection and Masking | Time complexity is O(n), where n is the length of the input string. Space complexity is O(1) as we are not using extra space relative to input size beyond required variables. |
| Regular Expression for Detecting Number of Digits | Time complexity is O(n), the complexity is driven by the initial parsing of the string. Space complexity is O(n) due to storage of the digits string. |
I solved 541 Leetcode problems #leetcode #codinginterview • Sahil & Sarra • 1,196,598 views views
Watch 9 more video solutions →Practice Masking Personal Information with our built-in code editor and test cases.
Practice on FleetCode