Skip to content

Commit fc9a039

Browse files
committed
Add solution and test-cases for problem 831
1 parent 1fbf15a commit fc9a039

File tree

3 files changed

+125
-22
lines changed

3 files changed

+125
-22
lines changed

leetcode/801-900/0831.Masking-Personal-Information/README.md

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,71 @@
11
# [831.Masking Personal Information][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
**Email address**:
7+
8+
An email address is:
9+
10+
- A **name** consisting of uppercase and lowercase English letters, followed by
11+
- The `'@'` symbol, followed by
12+
- The **domain** consisting of uppercase and lowercase English letters with a dot `'.'` somewhere in the middle (not the first or last character).
13+
14+
To mask an email:
15+
16+
- The uppercase letters in the **name** and **domain** must be converted to lowercase letters.
17+
- The middle letters of the **name** (i.e., all but the first and last letters) must be replaced by 5 asterisks `"*****"`.
18+
19+
**Phone number**:
20+
21+
A phone number is formatted as follows:
22+
23+
- The phone number contains 10-13 digits.
24+
- The last 10 digits make up the **local number**.
25+
- The remaining 0-3 digits, in the beginning, make up the **country code**.
26+
- **Separation characters** from the set `{'+', '-', '(', ')', ' '}` separate the above digits in some way.
27+
28+
To mask a phone number:
29+
30+
- Remove all **separation characters**.
31+
- The masked phone number should have the form:
32+
33+
- `"***-***-XXXX"` if the country code has 0 digits.
34+
- `"+*-***-***-XXXX"` if the country code has 1 digit.
35+
- `"+**-***-***-XXXX"` if the country code has 2 digits.
36+
- `"+***-***-***-XXXX"` if the country code has 3 digits.
37+
38+
- `"XXXX"` is the last 4 digits of the **local number**.
39+
740

841
**Example 1:**
942

1043
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
44+
Input: s = "LeetCode@LeetCode.com"
45+
Output: "l*****e@leetcode.com"
46+
Explanation: s is an email address.
47+
The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
1348
```
1449

15-
## 题意
16-
> ...
17-
18-
## 题解
50+
**Example 2:**
1951

20-
### 思路1
21-
> ...
22-
Masking Personal Information
23-
```go
2452
```
53+
Input: s = "AB@qq.com"
54+
Output: "a*****b@qq.com"
55+
Explanation: s is an email address.
56+
The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
57+
Note that even though "ab" is 2 characters, it still must have 5 asterisks in the middle.
58+
```
59+
60+
**Example 3:**
2561

62+
```
63+
Input: s = "1(234)567-890"
64+
Output: "***-***-7890"
65+
Explanation: s is a phone number.
66+
There are 10 digits, so the local number is 10 digits and the country code is 0 digits.
67+
Thus, the resulting masked number is "***-***-7890".
68+
```
2669

2770
## 结语
2871

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func maskEmail(email string) string {
6+
buf := strings.Builder{}
7+
cur := email[0]
8+
if cur >= 'A' && cur <= 'Z' {
9+
cur += uint8(32)
10+
}
11+
buf.WriteByte(cur)
12+
buf.WriteString("*****")
13+
i := 1
14+
for ; i < len(email) && email[i] != '@'; i++ {
15+
}
16+
cur = email[i-1]
17+
if cur >= 'A' && cur <= 'Z' {
18+
cur += uint8(32)
19+
}
20+
buf.WriteByte(cur)
21+
buf.WriteString(strings.ToLower(email[i:]))
22+
return buf.String()
23+
}
24+
25+
func maskPhone(phone string) string {
26+
buf := make([]byte, 0)
27+
i := len(phone) - 1
28+
29+
for ; i >= 0; i-- {
30+
if phone[i] >= '0' && phone[i] <= '9' {
31+
buf = append(buf, phone[i])
32+
}
33+
}
34+
ans := strings.Builder{}
35+
diff := len(buf) - 10
36+
if r := diff; r > 0 {
37+
ans.WriteByte('+')
38+
for ; r > 0; r-- {
39+
ans.WriteByte('*')
40+
}
41+
ans.WriteByte('-')
42+
}
43+
// 1, 2, 3, 4-5, 6, 7-8, 9, 10-11, 12,13
44+
index := len(buf) - diff - 1
45+
count := 3
46+
for ; index > 3; index-- {
47+
ans.WriteByte('*')
48+
count--
49+
if count == 0 {
50+
ans.WriteByte('-')
51+
count = 3
52+
}
53+
}
54+
for ; index >= 0; index-- {
55+
ans.WriteByte(buf[index])
56+
}
57+
return ans.String()
58+
}
59+
60+
func Solution(s string) string {
61+
if strings.Contains(s, "@") {
62+
return maskEmail(s)
63+
}
64+
return maskPhone(s)
565
}

leetcode/801-900/0831.Masking-Personal-Information/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "LeetCode@LeetCode.com", "l*****e@leetcode.com"},
17+
{"TestCase2", "AB@qq.com", "a*****b@qq.com"},
18+
{"TestCase3", "1(234)567-890", "***-***-7890"},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)