From 6fdf7cfd2d54c5e56d633b31b6596e4c05ed9c69 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 2 Oct 2025 14:11:40 +0800 Subject: [PATCH] Add solution and test-cases for problem 3527 --- .../README.md | 35 ++++++++++++------- .../Solution.go | 30 ++++++++++++++-- .../Solution_test.go | 13 ++++--- 3 files changed, 56 insertions(+), 22 deletions(-) diff --git a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/README.md b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/README.md index fbf3d0959..eac7e4755 100755 --- a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/README.md +++ b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution.go b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution.go index d115ccf5e..82f681cda 100644 --- a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution.go +++ b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution.go @@ -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 } diff --git a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution_test.go b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution_test.go index 14ff50eb4..039e61a84 100644 --- a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution_test.go +++ b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution_test.go @@ -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"}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }