diff --git a/solution/3500-3599/3527.Find the Most Common Response/README.md b/solution/3500-3599/3527.Find the Most Common Response/README.md index 9c75b3db5cf6e..59f9562e4ac4a 100644 --- a/solution/3500-3599/3527.Find the Most Common Response/README.md +++ b/solution/3500-3599/3527.Find the Most Common Response/README.md @@ -74,32 +74,134 @@ tags: -### 方法一 +### 方法一:哈希表 + +我们可以用一个哈希表 $\textit{cnt}$ 来统计每个回答的出现次数。对于每一天的回答,我们先去重,然后将每个回答加入哈希表中,更新其出现次数。 + +最后,我们遍历哈希表,找到出现次数最多的回答。如果有多个回答出现次数相同,则返回字典序最小的那个回答。 + +时间复杂度 $O(L)$,空间复杂度 $O(L)$。其中 $L$ 是所有回答的总长度。 #### Python3 ```python - +class Solution: + def findCommonResponse(self, responses: List[List[str]]) -> str: + cnt = Counter() + for ws in responses: + for w in set(ws): + cnt[w] += 1 + ans = responses[0][0] + for w, x in cnt.items(): + if cnt[ans] < x or (cnt[ans] == x and w < ans): + ans = w + return ans ``` #### Java ```java - +class Solution { + public String findCommonResponse(List> responses) { + Map cnt = new HashMap<>(); + for (var ws : responses) { + Set s = new HashSet<>(); + for (var w : ws) { + if (s.add(w)) { + cnt.merge(w, 1, Integer::sum); + } + } + } + String ans = responses.get(0).get(0); + for (var e : cnt.entrySet()) { + String w = e.getKey(); + int v = e.getValue(); + if (cnt.get(ans) < v || (cnt.get(ans) == v && w.compareTo(ans) < 0)) { + ans = w; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + string findCommonResponse(vector>& responses) { + unordered_map cnt; + for (const auto& ws : responses) { + unordered_set s; + for (const auto& w : ws) { + if (s.insert(w).second) { + ++cnt[w]; + } + } + } + string ans = responses[0][0]; + for (const auto& e : cnt) { + const string& w = e.first; + int v = e.second; + if (cnt[ans] < v || (cnt[ans] == v && w < ans)) { + ans = w; + } + } + return ans; + } +}; ``` #### Go ```go +func findCommonResponse(responses [][]string) string { + cnt := map[string]int{} + for _, ws := range responses { + s := map[string]struct{}{} + for _, w := range ws { + if _, ok := s[w]; !ok { + s[w] = struct{}{} + cnt[w]++ + } + } + } + ans := responses[0][0] + for w, v := range cnt { + if cnt[ans] < v || (cnt[ans] == v && w < ans) { + ans = w + } + } + return ans +} +``` +#### TypeScript + +```ts +function findCommonResponse(responses: string[][]): string { + const cnt = new Map(); + for (const ws of responses) { + const s = new Set(); + for (const w of ws) { + if (!s.has(w)) { + s.add(w); + cnt.set(w, (cnt.get(w) ?? 0) + 1); + } + } + } + let ans = responses[0][0]; + for (const [w, v] of cnt) { + const best = cnt.get(ans)!; + if (best < v || (best === v && w < ans)) { + ans = w; + } + } + return ans; +} ``` diff --git a/solution/3500-3599/3527.Find the Most Common Response/README_EN.md b/solution/3500-3599/3527.Find the Most Common Response/README_EN.md index dd6d7bd474380..ab7496b0924ff 100644 --- a/solution/3500-3599/3527.Find the Most Common Response/README_EN.md +++ b/solution/3500-3599/3527.Find the Most Common Response/README_EN.md @@ -72,32 +72,134 @@ tags: -### Solution 1 +### Solution 1: Hash Table + +We can use a hash table $\textit{cnt}$ to count the occurrences of each response. For the responses of each day, we first remove duplicates, then add each response to the hash table and update its count. + +Finally, we iterate through the hash table to find the response with the highest count. If there are multiple responses with the same count, we return the lexicographically smallest one. + +The complexity is $O(L)$, and the space complexity is $O(L)$, where $L$ is the total length of all responses. #### Python3 ```python - +class Solution: + def findCommonResponse(self, responses: List[List[str]]) -> str: + cnt = Counter() + for ws in responses: + for w in set(ws): + cnt[w] += 1 + ans = responses[0][0] + for w, x in cnt.items(): + if cnt[ans] < x or (cnt[ans] == x and w < ans): + ans = w + return ans ``` #### Java ```java - +class Solution { + public String findCommonResponse(List> responses) { + Map cnt = new HashMap<>(); + for (var ws : responses) { + Set s = new HashSet<>(); + for (var w : ws) { + if (s.add(w)) { + cnt.merge(w, 1, Integer::sum); + } + } + } + String ans = responses.get(0).get(0); + for (var e : cnt.entrySet()) { + String w = e.getKey(); + int v = e.getValue(); + if (cnt.get(ans) < v || (cnt.get(ans) == v && w.compareTo(ans) < 0)) { + ans = w; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + string findCommonResponse(vector>& responses) { + unordered_map cnt; + for (const auto& ws : responses) { + unordered_set s; + for (const auto& w : ws) { + if (s.insert(w).second) { + ++cnt[w]; + } + } + } + string ans = responses[0][0]; + for (const auto& e : cnt) { + const string& w = e.first; + int v = e.second; + if (cnt[ans] < v || (cnt[ans] == v && w < ans)) { + ans = w; + } + } + return ans; + } +}; ``` #### Go ```go +func findCommonResponse(responses [][]string) string { + cnt := map[string]int{} + for _, ws := range responses { + s := map[string]struct{}{} + for _, w := range ws { + if _, ok := s[w]; !ok { + s[w] = struct{}{} + cnt[w]++ + } + } + } + ans := responses[0][0] + for w, v := range cnt { + if cnt[ans] < v || (cnt[ans] == v && w < ans) { + ans = w + } + } + return ans +} +``` +#### TypeScript + +```ts +function findCommonResponse(responses: string[][]): string { + const cnt = new Map(); + for (const ws of responses) { + const s = new Set(); + for (const w of ws) { + if (!s.has(w)) { + s.add(w); + cnt.set(w, (cnt.get(w) ?? 0) + 1); + } + } + } + let ans = responses[0][0]; + for (const [w, v] of cnt) { + const best = cnt.get(ans)!; + if (best < v || (best === v && w < ans)) { + ans = w; + } + } + return ans; +} ``` diff --git a/solution/3500-3599/3527.Find the Most Common Response/Solution.cpp b/solution/3500-3599/3527.Find the Most Common Response/Solution.cpp new file mode 100644 index 0000000000000..cae7231ee1390 --- /dev/null +++ b/solution/3500-3599/3527.Find the Most Common Response/Solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + string findCommonResponse(vector>& responses) { + unordered_map cnt; + for (const auto& ws : responses) { + unordered_set s; + for (const auto& w : ws) { + if (s.insert(w).second) { + ++cnt[w]; + } + } + } + string ans = responses[0][0]; + for (const auto& e : cnt) { + const string& w = e.first; + int v = e.second; + if (cnt[ans] < v || (cnt[ans] == v && w < ans)) { + ans = w; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3527.Find the Most Common Response/Solution.go b/solution/3500-3599/3527.Find the Most Common Response/Solution.go new file mode 100644 index 0000000000000..516019661b7f4 --- /dev/null +++ b/solution/3500-3599/3527.Find the Most Common Response/Solution.go @@ -0,0 +1,19 @@ +func findCommonResponse(responses [][]string) string { + cnt := map[string]int{} + for _, ws := range responses { + s := map[string]struct{}{} + for _, w := range ws { + if _, ok := s[w]; !ok { + s[w] = struct{}{} + cnt[w]++ + } + } + } + ans := responses[0][0] + for w, v := range cnt { + if cnt[ans] < v || (cnt[ans] == v && w < ans) { + ans = w + } + } + return ans +} diff --git a/solution/3500-3599/3527.Find the Most Common Response/Solution.java b/solution/3500-3599/3527.Find the Most Common Response/Solution.java new file mode 100644 index 0000000000000..4aaa5ac15e776 --- /dev/null +++ b/solution/3500-3599/3527.Find the Most Common Response/Solution.java @@ -0,0 +1,22 @@ +class Solution { + public String findCommonResponse(List> responses) { + Map cnt = new HashMap<>(); + for (var ws : responses) { + Set s = new HashSet<>(); + for (var w : ws) { + if (s.add(w)) { + cnt.merge(w, 1, Integer::sum); + } + } + } + String ans = responses.get(0).get(0); + for (var e : cnt.entrySet()) { + String w = e.getKey(); + int v = e.getValue(); + if (cnt.get(ans) < v || (cnt.get(ans) == v && w.compareTo(ans) < 0)) { + ans = w; + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3527.Find the Most Common Response/Solution.py b/solution/3500-3599/3527.Find the Most Common Response/Solution.py new file mode 100644 index 0000000000000..d6072ed1bd257 --- /dev/null +++ b/solution/3500-3599/3527.Find the Most Common Response/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def findCommonResponse(self, responses: List[List[str]]) -> str: + cnt = Counter() + for ws in responses: + for w in set(ws): + cnt[w] += 1 + ans = responses[0][0] + for w, x in cnt.items(): + if cnt[ans] < x or (cnt[ans] == x and w < ans): + ans = w + return ans diff --git a/solution/3500-3599/3527.Find the Most Common Response/Solution.ts b/solution/3500-3599/3527.Find the Most Common Response/Solution.ts new file mode 100644 index 0000000000000..955b76346ec4e --- /dev/null +++ b/solution/3500-3599/3527.Find the Most Common Response/Solution.ts @@ -0,0 +1,20 @@ +function findCommonResponse(responses: string[][]): string { + const cnt = new Map(); + for (const ws of responses) { + const s = new Set(); + for (const w of ws) { + if (!s.has(w)) { + s.add(w); + cnt.set(w, (cnt.get(w) ?? 0) + 1); + } + } + } + let ans = responses[0][0]; + for (const [w, v] of cnt) { + const best = cnt.get(ans)!; + if (best < v || (best === v && w < ans)) { + ans = w; + } + } + return ans; +}