Skip to content
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
35 changes: 22 additions & 13 deletions leetcode/3501-3600/3527.Find-the-Most-Common-Response/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
# [3527.Find the Most Common Response][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 2D string array `responses` where each `responses[i]` is an array of strings representing survey responses from the `ith` day.

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.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: responses = [["good","ok","good","ok"],["ok","bad","good","ok","ok"],["good"],["bad"]]

## 题意
> ...
Output: "good"

## 题解
Explanation:

### 思路1
> ...
Find the Most Common Response
```go
After removing duplicates within each list, responses = [["good", "ok"], ["ok", "bad", "good"], ["good"], ["bad"]].
"good" appears 3 times, "ok" appears 2 times, and "bad" appears 2 times.
Return "good" because it has the highest frequency.
```

**Example 2:**

```
Input: responses = [["good","ok","good"],["ok","bad"],["bad","notsure"],["great","good"]]

Output: "bad"

Explanation:

After removing duplicates within each list we have responses = [["good", "ok"], ["ok", "bad"], ["bad", "notsure"], ["great", "good"]].
"bad", "good", and "ok" each occur 2 times.
The output is "bad" because it is the lexicographically smallest amongst the words with the highest frequency.
```

## 结语

Expand Down
30 changes: 28 additions & 2 deletions leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(responses [][]string) string {
store := make(map[string]int)
for _, resp := range responses {
cur := make(map[string]struct{})
for _, str := range resp {
cur[str] = struct{}{}
}
for key := range cur {
store[key]++
}
}
var (
ret string
cnt int
)
for key, count := range store {
if count > cnt {
ret = key
cnt = count
continue
}

if count == cnt {
ret = min(ret, key)
}
}

return ret
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ 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", [][]string{{"good", "ok", "good", "ok"}, {"ok", "bad", "good", "ok", "ok"}, {"good"}, {"bad"}}, "good"},
{"TestCase2", [][]string{{"good", "ok", "good"}, {"ok", "bad"}, {"bad", "notsure"}, {"great", "good"}}, "bad"},
}

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

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

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