Skip to content
Open
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
46 changes: 33 additions & 13 deletions leetcode/3601-3700/3606.Coupon-Code-Validator/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
# [3606.Coupon Code Validator][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 three arrays of length n that describe the properties of `n` coupons: `code`, `businessLine`, and `isActive`. The `ith` coupon has:

- `code[i]`: a **string** representing the coupon identifier.
- `businessLine[i]`: a **string** denoting the business category of the coupon.
- `isActive[i]`: a **boolean** indicating whether the coupon is currently active.

A coupon is considered **valid** if all of the following conditions hold:

- `code[i]` is non-empty and consists only of alphanumeric characters (a-z, A-Z, 0-9) and underscores (`_`).
- `businessLine[i]` is one of the following four categories: `"electronics"`, `"grocery`, `"pharmacy`, `"restaurant"`.
- `isActive[i]` is **true**.

Return an array of the **codes***codes** all valid coupons, **sorted** first by their **businessLine** in the order: `"electronics"`, `"grocery"`, `"pharmacy"`, `"restaurant"`, and then by **code** in lexicographical (ascending) order within each category.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: code = ["SAVE20","","PHARMA5","SAVE@20"], businessLine = ["restaurant","grocery","pharmacy","restaurant"], isActive = [true,true,true,true]

## 题意
> ...
Output: ["PHARMA5","SAVE20"]

## 题解
Explanation:

### 思路1
> ...
Coupon Code Validator
```go
First coupon is valid.
Second coupon has empty code (invalid).
Third coupon is valid.
Fourth coupon has special character @ (invalid).
```

**Example 2:**

```
Input: code = ["GROCERY15","ELECTRONICS_50","DISCOUNT10"], businessLine = ["grocery","electronics","invalid"], isActive = [false,true,true]

Output: ["ELECTRONICS_50"]

Explanation:

First coupon is inactive (invalid).
Second coupon is valid.
Third coupon has invalid business line (invalid).
```

## 结语

Expand Down
43 changes: 41 additions & 2 deletions leetcode/3601-3700/3606.Coupon-Code-Validator/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
package Solution

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

func validateCode(str string) bool {
for _, b := range str {
if !(b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= '0' && b <= '9' || b == '_') {
return false
}
}
return len(str) > 0
}

func Solution(code []string, businessLine []string, isActive []bool) []string {

indies := make([]int, 0)
order := map[string]int{
"electronics": 1, "grocery": 2, "pharmacy": 3, "restaurant": 4,
}
for index := range code {
if !validateCode(code[index]) || !isActive[index] {
continue
}
if _, ok := order[businessLine[index]]; !ok {
continue
}
indies = append(indies, index)
}

sort.Slice(indies, func(i, j int) bool {
a, b := businessLine[indies[i]], businessLine[indies[j]]
if a == b {
return code[indies[i]] < code[indies[j]]
}
return order[a] < order[b]
})

ret := make([]string, len(indies))
for i := range indies {
ret[i] = code[indies[i]]
}

return ret
}
22 changes: 11 additions & 11 deletions leetcode/3601-3700/3606.Coupon-Code-Validator/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
code, businessLine []string
isActive []bool
expect []string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"SAVE20", "", "PHARMA5", "SAVE@20"}, []string{"restaurant", "grocery", "pharmacy", "restaurant"}, []bool{true, true, true, true}, []string{"PHARMA5", "SAVE20"}},
{"TestCase2", []string{"GROCERY15", "ELECTRONICS_50", "DISCOUNT10"}, []string{"grocery", "electronics", "invalid"}, []bool{false, true, true}, []string{"ELECTRONICS_50"}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.code, c.businessLine, c.isActive)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.code, c.businessLine, c.isActive)
}
})
}
}

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

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