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