Skip to content

Commit 6fdf7cf

Browse files
committed
Add solution and test-cases for problem 3527
1 parent f03226b commit 6fdf7cf

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

leetcode/3501-3600/3527.Find-the-Most-Common-Response/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [3527.Find the Most Common Response][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 2D string array `responses` where each `responses[i]` is an array of strings representing survey responses from the `ith` day.
5+
6+
Return the **most common** response across all days after removing **duplicate** responses within each `responses[i]`. If there is a tie, return the lexicographically smallest response.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
11+
Input: responses = [["good","ok","good","ok"],["ok","bad","good","ok","ok"],["good"],["bad"]]
1412
15-
## 题意
16-
> ...
13+
Output: "good"
1714
18-
## 题解
15+
Explanation:
1916
20-
### 思路1
21-
> ...
22-
Find the Most Common Response
23-
```go
17+
After removing duplicates within each list, responses = [["good", "ok"], ["ok", "bad", "good"], ["good"], ["bad"]].
18+
"good" appears 3 times, "ok" appears 2 times, and "bad" appears 2 times.
19+
Return "good" because it has the highest frequency.
2420
```
2521

22+
**Example 2:**
23+
24+
```
25+
Input: responses = [["good","ok","good"],["ok","bad"],["bad","notsure"],["great","good"]]
26+
27+
Output: "bad"
28+
29+
Explanation:
30+
31+
After removing duplicates within each list we have responses = [["good", "ok"], ["ok", "bad"], ["bad", "notsure"], ["great", "good"]].
32+
"bad", "good", and "ok" each occur 2 times.
33+
The output is "bad" because it is the lexicographically smallest amongst the words with the highest frequency.
34+
```
2635

2736
## 结语
2837

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(responses [][]string) string {
4+
store := make(map[string]int)
5+
for _, resp := range responses {
6+
cur := make(map[string]struct{})
7+
for _, str := range resp {
8+
cur[str] = struct{}{}
9+
}
10+
for key := range cur {
11+
store[key]++
12+
}
13+
}
14+
var (
15+
ret string
16+
cnt int
17+
)
18+
for key, count := range store {
19+
if count > cnt {
20+
ret = key
21+
cnt = count
22+
continue
23+
}
24+
25+
if count == cnt {
26+
ret = min(ret, key)
27+
}
28+
}
29+
30+
return ret
531
}

leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ 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", [][]string{{"good", "ok", "good", "ok"}, {"ok", "bad", "good", "ok", "ok"}, {"good"}, {"bad"}}, "good"},
17+
{"TestCase2", [][]string{{"good", "ok", "good"}, {"ok", "bad"}, {"bad", "notsure"}, {"great", "good"}}, "bad"},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
//压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
//使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)