Skip to content

Commit c9fa5c7

Browse files
committed
Add solution #831
1 parent 59ca0e5 commit c9fa5c7

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
804|[Unique Morse Code Words](./0804-unique-morse-code-words.js)|Easy|
3030
819|[Most Common Word](./0819-most-common-word.js)|Easy|
3131
824|[Goat Latin](./0824-goat-latin.js)|Easy|
32+
831|[Masking Personal Information](./0831-masking-personal-information.js)|Medium|
3233
890|[Find and Replace Pattern](./0890-find-and-replace-pattern.js)|Medium|
3334
916|[Word Subsets](./0916-word-subsets.js)|Medium|
3435
929|[Unique Email Addresses](./0929-unique-email-addresses.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* 831. Masking Personal Information
3+
* https://leetcode.com/problems/masking-personal-information/
4+
* Difficulty: Medium
5+
*
6+
* We are given a personal information string S, which may represent either an email address or a phone number.
7+
* We would like to mask this personal information according to the following rules:
8+
* 1. Email address:
9+
* We define a name to be a string of length ≥ 2 consisting of only lowercase letters a-z or uppercase letters A-Z.
10+
* An email address starts with a name, followed by the symbol '@', followed by a name, followed by the dot '.' and
11+
* followed by a name. All email addresses are guaranteed to be valid and in the format of "[email protected]".
12+
* To mask an email, all names must be converted to lowercase and all letters between the first and last letter of
13+
* the first name must be replaced by 5 asterisks '*'.
14+
*
15+
* 2. Phone number:
16+
* A phone number is a string consisting of only the digits 0-9 or the characters from the set
17+
* {'+', '-', '(', ')', ' '}. You may assume a phone number contains 10 to 13 digits. The last 10 digits make up the
18+
* local number, while the digits before those make up the country code. Note that the country code is optional. We
19+
* want to expose only the last 4 digits and mask all other digits. The local number should be formatted and masked
20+
* as "***-***-1111", where 1 represents the exposed digits. To mask a phone number with country code like
21+
* "+111 111 111 1111", we write it in the form "+***-***-***-1111". The '+' sign and the first '-' sign before the
22+
* local number should only exist if there is a country code. For example, a 12 digit phone number mask should start
23+
* with "+**-". Note that extraneous characters like "(", ")", " ", as well as extra dashes or plus signs not part of
24+
* the above formatting scheme should be removed.
25+
*
26+
* Return the correct "mask" of the information provided.
27+
*/
28+
29+
// The one-line solution:
30+
/**
31+
* @param {string} S
32+
* @return {string}
33+
*/
34+
var maskPII = function(S) {
35+
return S.toLowerCase()
36+
.replace(/^([a-z])[a-z]*([a-z])/, '$1*****$2')
37+
.replace(/[)(\-\s+]+/g, '')
38+
.replace(/^(\d*)(\d{3})(\d{3})(\d{4})$/, '+$1-$2-$3-$4')
39+
.replace(/\d(?!\d{0,3}$)/g, '*')
40+
.replace(/^\+\-/, '');
41+
};
42+
43+
// More readable alternative:
44+
/**
45+
* @param {string} S
46+
* @return {string}
47+
*/
48+
var maskPII = function(S) {
49+
if (S.includes('@')) {
50+
return S.toLowerCase().replace(/^(\w)\w*(\w)/, '$1*****$2');
51+
} else {
52+
return S.replace(/\D+/g, '')
53+
.replace(/^(\d*)(\d{3})(\d{3})(\d{4})$/, '+$1-$2-$3-$4')
54+
.replace(/\d(?!\d{0,3}$)/g, '*')
55+
.replace(/^\+\-/, '');
56+
}
57+
};

0 commit comments

Comments
 (0)