Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 831 #985

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 56 additions & 13 deletions leetcode/801-900/0831.Masking-Personal-Information/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,71 @@
# [831.Masking Personal Information][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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:

- A **name** consisting of uppercase and lowercase English letters, followed by
- The `'@'` symbol, followed by
- The **domain** consisting of uppercase and lowercase English letters with a dot `'.'` somewhere in the middle (not the first or last character).

To mask an email:

- The uppercase letters in the **name** and **domain** must be converted to lowercase letters.
- The middle letters of the **name** (i.e., all but the first and last letters) must be replaced by 5 asterisks `"*****"`.

**Phone number**:

A phone number is formatted as follows:

- The phone number contains 10-13 digits.
- The last 10 digits make up the **local number**.
- The remaining 0-3 digits, in the beginning, make up the **country code**.
- **Separation characters** from the set `{'+', '-', '(', ')', ' '}` separate the above digits in some way.

To mask a phone number:

- Remove all **separation characters**.
- The masked phone number should have the form:

- `"***-***-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: a = "11", b = "1"
Output: "100"
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:**

### 思路1
> ...
Masking Personal Information
```go
```
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".
```

## 结语

Expand Down
64 changes: 62 additions & 2 deletions leetcode/801-900/0831.Masking-Personal-Information/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
package Solution

func Solution(x bool) bool {
return x
import "strings"

func maskEmail(email string) string {
buf := strings.Builder{}
cur := email[0]
if cur >= 'A' && cur <= 'Z' {
cur += uint8(32)
}
buf.WriteByte(cur)
buf.WriteString("*****")
i := 1
for ; i < len(email) && email[i] != '@'; i++ {
}
cur = email[i-1]
if cur >= 'A' && cur <= 'Z' {
cur += uint8(32)
}
buf.WriteByte(cur)
buf.WriteString(strings.ToLower(email[i:]))
return buf.String()
}

func maskPhone(phone string) string {
buf := make([]byte, 0)
i := len(phone) - 1

for ; i >= 0; i-- {
if phone[i] >= '0' && phone[i] <= '9' {
buf = append(buf, phone[i])
}
}
ans := strings.Builder{}
diff := len(buf) - 10
if r := diff; r > 0 {
ans.WriteByte('+')
for ; r > 0; r-- {
ans.WriteByte('*')
}
ans.WriteByte('-')
}
// 1, 2, 3, 4-5, 6, 7-8, 9, 10-11, 12,13
index := len(buf) - diff - 1
count := 3
for ; index > 3; index-- {
ans.WriteByte('*')
count--
if count == 0 {
ans.WriteByte('-')
count = 3
}
}
for ; index >= 0; index-- {
ans.WriteByte(buf[index])
}
return ans.String()
}

func Solution(s string) string {
if strings.Contains(s, "@") {
return maskEmail(s)
}
return maskPhone(s)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "LeetCode@LeetCode.com", "l*****e@leetcode.com"},
{"TestCase2", "AB@qq.com", "a*****b@qq.com"},
{"TestCase3", "1(234)567-890", "***-***-7890"},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading