Skip to content

Commit 7676cac

Browse files
committed
Add solution and test-cases for problem 3606
1 parent 7ce34f6 commit 7676cac

File tree

3 files changed

+85
-26
lines changed

3 files changed

+85
-26
lines changed

leetcode/3601-3700/3606.Coupon-Code-Validator/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [3606.Coupon Code Validator][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 three arrays of length n that describe the properties of `n` coupons: `code`, `businessLine`, and `isActive`. The `ith` coupon has:
5+
6+
- `code[i]`: a **string** representing the coupon identifier.
7+
- `businessLine[i]`: a **string** denoting the business category of the coupon.
8+
- `isActive[i]`: a **boolean** indicating whether the coupon is currently active.
9+
10+
A coupon is considered **valid** if all of the following conditions hold:
11+
12+
- `code[i]` is non-empty and consists only of alphanumeric characters (a-z, A-Z, 0-9) and underscores (`_`).
13+
- `businessLine[i]` is one of the following four categories: `"electronics"`, `"grocery`, `"pharmacy`, `"restaurant"`.
14+
- `isActive[i]` is **true**.
15+
16+
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.
717

818
**Example 1:**
919

1020
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
21+
Input: code = ["SAVE20","","PHARMA5","SAVE@20"], businessLine = ["restaurant","grocery","pharmacy","restaurant"], isActive = [true,true,true,true]
1422
15-
## 题意
16-
> ...
23+
Output: ["PHARMA5","SAVE20"]
1724
18-
## 题解
25+
Explanation:
1926
20-
### 思路1
21-
> ...
22-
Coupon Code Validator
23-
```go
27+
First coupon is valid.
28+
Second coupon has empty code (invalid).
29+
Third coupon is valid.
30+
Fourth coupon has special character @ (invalid).
2431
```
2532

33+
**Example 2:**
34+
35+
```
36+
Input: code = ["GROCERY15","ELECTRONICS_50","DISCOUNT10"], businessLine = ["grocery","electronics","invalid"], isActive = [false,true,true]
37+
38+
Output: ["ELECTRONICS_50"]
39+
40+
Explanation:
41+
42+
First coupon is inactive (invalid).
43+
Second coupon is valid.
44+
Third coupon has invalid business line (invalid).
45+
```
2646

2747
## 结语
2848

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func validateCode(str string) bool {
6+
for _, b := range str {
7+
if !(b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= '0' && b <= '9' || b == '_') {
8+
return false
9+
}
10+
}
11+
return len(str) > 0
12+
}
13+
14+
func Solution(code []string, businessLine []string, isActive []bool) []string {
15+
16+
indies := make([]int, 0)
17+
order := map[string]int{
18+
"electronics": 1, "grocery": 2, "pharmacy": 3, "restaurant": 4,
19+
}
20+
for index := range code {
21+
if !validateCode(code[index]) || !isActive[index] {
22+
continue
23+
}
24+
if _, ok := order[businessLine[index]]; !ok {
25+
continue
26+
}
27+
indies = append(indies, index)
28+
}
29+
30+
sort.Slice(indies, func(i, j int) bool {
31+
a, b := businessLine[indies[i]], businessLine[indies[j]]
32+
if a == b {
33+
return code[indies[i]] < code[indies[j]]
34+
}
35+
return order[a] < order[b]
36+
})
37+
38+
ret := make([]string, len(indies))
39+
for i := range indies {
40+
ret[i] = code[indies[i]]
41+
}
42+
43+
return ret
544
}

leetcode/3601-3700/3606.Coupon-Code-Validator/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
code, businessLine []string
14+
isActive []bool
15+
expect []string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []string{"SAVE20", "", "PHARMA5", "SAVE@20"}, []string{"restaurant", "grocery", "pharmacy", "restaurant"}, []bool{true, true, true, true}, []string{"PHARMA5", "SAVE20"}},
18+
{"TestCase2", []string{"GROCERY15", "ELECTRONICS_50", "DISCOUNT10"}, []string{"grocery", "electronics", "invalid"}, []bool{false, true, true}, []string{"ELECTRONICS_50"}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.code, c.businessLine, c.isActive)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.code, c.businessLine, c.isActive)
2828
}
2929
})
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)