Skip to content

Commit 39eaf17

Browse files
authored
feat: add solutions to lc problem: No.3527 (#4381)
No.3527.Find the Most Common Response
1 parent 7d76370 commit 39eaf17

File tree

7 files changed

+307
-8
lines changed

7 files changed

+307
-8
lines changed

solution/3500-3599/3527.Find the Most Common Response/README.md

+106-4
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,134 @@ tags:
7474

7575
<!-- solution:start -->
7676

77-
### 方法一
77+
### 方法一:哈希表
78+
79+
我们可以用一个哈希表 $\textit{cnt}$ 来统计每个回答的出现次数。对于每一天的回答,我们先去重,然后将每个回答加入哈希表中,更新其出现次数。
80+
81+
最后,我们遍历哈希表,找到出现次数最多的回答。如果有多个回答出现次数相同,则返回字典序最小的那个回答。
82+
83+
时间复杂度 $O(L)$,空间复杂度 $O(L)$。其中 $L$ 是所有回答的总长度。
7884

7985
<!-- tabs:start -->
8086

8187
#### Python3
8288

8389
```python
84-
90+
class Solution:
91+
def findCommonResponse(self, responses: List[List[str]]) -> str:
92+
cnt = Counter()
93+
for ws in responses:
94+
for w in set(ws):
95+
cnt[w] += 1
96+
ans = responses[0][0]
97+
for w, x in cnt.items():
98+
if cnt[ans] < x or (cnt[ans] == x and w < ans):
99+
ans = w
100+
return ans
85101
```
86102

87103
#### Java
88104

89105
```java
90-
106+
class Solution {
107+
public String findCommonResponse(List<List<String>> responses) {
108+
Map<String, Integer> cnt = new HashMap<>();
109+
for (var ws : responses) {
110+
Set<String> s = new HashSet<>();
111+
for (var w : ws) {
112+
if (s.add(w)) {
113+
cnt.merge(w, 1, Integer::sum);
114+
}
115+
}
116+
}
117+
String ans = responses.get(0).get(0);
118+
for (var e : cnt.entrySet()) {
119+
String w = e.getKey();
120+
int v = e.getValue();
121+
if (cnt.get(ans) < v || (cnt.get(ans) == v && w.compareTo(ans) < 0)) {
122+
ans = w;
123+
}
124+
}
125+
return ans;
126+
}
127+
}
91128
```
92129

93130
#### C++
94131

95132
```cpp
96-
133+
class Solution {
134+
public:
135+
string findCommonResponse(vector<vector<string>>& responses) {
136+
unordered_map<string, int> cnt;
137+
for (const auto& ws : responses) {
138+
unordered_set<string> s;
139+
for (const auto& w : ws) {
140+
if (s.insert(w).second) {
141+
++cnt[w];
142+
}
143+
}
144+
}
145+
string ans = responses[0][0];
146+
for (const auto& e : cnt) {
147+
const string& w = e.first;
148+
int v = e.second;
149+
if (cnt[ans] < v || (cnt[ans] == v && w < ans)) {
150+
ans = w;
151+
}
152+
}
153+
return ans;
154+
}
155+
};
97156
```
98157
99158
#### Go
100159
101160
```go
161+
func findCommonResponse(responses [][]string) string {
162+
cnt := map[string]int{}
163+
for _, ws := range responses {
164+
s := map[string]struct{}{}
165+
for _, w := range ws {
166+
if _, ok := s[w]; !ok {
167+
s[w] = struct{}{}
168+
cnt[w]++
169+
}
170+
}
171+
}
172+
ans := responses[0][0]
173+
for w, v := range cnt {
174+
if cnt[ans] < v || (cnt[ans] == v && w < ans) {
175+
ans = w
176+
}
177+
}
178+
return ans
179+
}
180+
```
102181

182+
#### TypeScript
183+
184+
```ts
185+
function findCommonResponse(responses: string[][]): string {
186+
const cnt = new Map<string, number>();
187+
for (const ws of responses) {
188+
const s = new Set<string>();
189+
for (const w of ws) {
190+
if (!s.has(w)) {
191+
s.add(w);
192+
cnt.set(w, (cnt.get(w) ?? 0) + 1);
193+
}
194+
}
195+
}
196+
let ans = responses[0][0];
197+
for (const [w, v] of cnt) {
198+
const best = cnt.get(ans)!;
199+
if (best < v || (best === v && w < ans)) {
200+
ans = w;
201+
}
202+
}
203+
return ans;
204+
}
103205
```
104206

105207
<!-- tabs:end -->

solution/3500-3599/3527.Find the Most Common Response/README_EN.md

+106-4
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,134 @@ tags:
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1
75+
### Solution 1: Hash Table
76+
77+
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.
78+
79+
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.
80+
81+
The complexity is $O(L)$, and the space complexity is $O(L)$, where $L$ is the total length of all responses.
7682

7783
<!-- tabs:start -->
7884

7985
#### Python3
8086

8187
```python
82-
88+
class Solution:
89+
def findCommonResponse(self, responses: List[List[str]]) -> str:
90+
cnt = Counter()
91+
for ws in responses:
92+
for w in set(ws):
93+
cnt[w] += 1
94+
ans = responses[0][0]
95+
for w, x in cnt.items():
96+
if cnt[ans] < x or (cnt[ans] == x and w < ans):
97+
ans = w
98+
return ans
8399
```
84100

85101
#### Java
86102

87103
```java
88-
104+
class Solution {
105+
public String findCommonResponse(List<List<String>> responses) {
106+
Map<String, Integer> cnt = new HashMap<>();
107+
for (var ws : responses) {
108+
Set<String> s = new HashSet<>();
109+
for (var w : ws) {
110+
if (s.add(w)) {
111+
cnt.merge(w, 1, Integer::sum);
112+
}
113+
}
114+
}
115+
String ans = responses.get(0).get(0);
116+
for (var e : cnt.entrySet()) {
117+
String w = e.getKey();
118+
int v = e.getValue();
119+
if (cnt.get(ans) < v || (cnt.get(ans) == v && w.compareTo(ans) < 0)) {
120+
ans = w;
121+
}
122+
}
123+
return ans;
124+
}
125+
}
89126
```
90127

91128
#### C++
92129

93130
```cpp
94-
131+
class Solution {
132+
public:
133+
string findCommonResponse(vector<vector<string>>& responses) {
134+
unordered_map<string, int> cnt;
135+
for (const auto& ws : responses) {
136+
unordered_set<string> s;
137+
for (const auto& w : ws) {
138+
if (s.insert(w).second) {
139+
++cnt[w];
140+
}
141+
}
142+
}
143+
string ans = responses[0][0];
144+
for (const auto& e : cnt) {
145+
const string& w = e.first;
146+
int v = e.second;
147+
if (cnt[ans] < v || (cnt[ans] == v && w < ans)) {
148+
ans = w;
149+
}
150+
}
151+
return ans;
152+
}
153+
};
95154
```
96155
97156
#### Go
98157
99158
```go
159+
func findCommonResponse(responses [][]string) string {
160+
cnt := map[string]int{}
161+
for _, ws := range responses {
162+
s := map[string]struct{}{}
163+
for _, w := range ws {
164+
if _, ok := s[w]; !ok {
165+
s[w] = struct{}{}
166+
cnt[w]++
167+
}
168+
}
169+
}
170+
ans := responses[0][0]
171+
for w, v := range cnt {
172+
if cnt[ans] < v || (cnt[ans] == v && w < ans) {
173+
ans = w
174+
}
175+
}
176+
return ans
177+
}
178+
```
100179

180+
#### TypeScript
181+
182+
```ts
183+
function findCommonResponse(responses: string[][]): string {
184+
const cnt = new Map<string, number>();
185+
for (const ws of responses) {
186+
const s = new Set<string>();
187+
for (const w of ws) {
188+
if (!s.has(w)) {
189+
s.add(w);
190+
cnt.set(w, (cnt.get(w) ?? 0) + 1);
191+
}
192+
}
193+
}
194+
let ans = responses[0][0];
195+
for (const [w, v] of cnt) {
196+
const best = cnt.get(ans)!;
197+
if (best < v || (best === v && w < ans)) {
198+
ans = w;
199+
}
200+
}
201+
return ans;
202+
}
101203
```
102204

103205
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
string findCommonResponse(vector<vector<string>>& responses) {
4+
unordered_map<string, int> cnt;
5+
for (const auto& ws : responses) {
6+
unordered_set<string> s;
7+
for (const auto& w : ws) {
8+
if (s.insert(w).second) {
9+
++cnt[w];
10+
}
11+
}
12+
}
13+
string ans = responses[0][0];
14+
for (const auto& e : cnt) {
15+
const string& w = e.first;
16+
int v = e.second;
17+
if (cnt[ans] < v || (cnt[ans] == v && w < ans)) {
18+
ans = w;
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func findCommonResponse(responses [][]string) string {
2+
cnt := map[string]int{}
3+
for _, ws := range responses {
4+
s := map[string]struct{}{}
5+
for _, w := range ws {
6+
if _, ok := s[w]; !ok {
7+
s[w] = struct{}{}
8+
cnt[w]++
9+
}
10+
}
11+
}
12+
ans := responses[0][0]
13+
for w, v := range cnt {
14+
if cnt[ans] < v || (cnt[ans] == v && w < ans) {
15+
ans = w
16+
}
17+
}
18+
return ans
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public String findCommonResponse(List<List<String>> responses) {
3+
Map<String, Integer> cnt = new HashMap<>();
4+
for (var ws : responses) {
5+
Set<String> s = new HashSet<>();
6+
for (var w : ws) {
7+
if (s.add(w)) {
8+
cnt.merge(w, 1, Integer::sum);
9+
}
10+
}
11+
}
12+
String ans = responses.get(0).get(0);
13+
for (var e : cnt.entrySet()) {
14+
String w = e.getKey();
15+
int v = e.getValue();
16+
if (cnt.get(ans) < v || (cnt.get(ans) == v && w.compareTo(ans) < 0)) {
17+
ans = w;
18+
}
19+
}
20+
return ans;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def findCommonResponse(self, responses: List[List[str]]) -> str:
3+
cnt = Counter()
4+
for ws in responses:
5+
for w in set(ws):
6+
cnt[w] += 1
7+
ans = responses[0][0]
8+
for w, x in cnt.items():
9+
if cnt[ans] < x or (cnt[ans] == x and w < ans):
10+
ans = w
11+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function findCommonResponse(responses: string[][]): string {
2+
const cnt = new Map<string, number>();
3+
for (const ws of responses) {
4+
const s = new Set<string>();
5+
for (const w of ws) {
6+
if (!s.has(w)) {
7+
s.add(w);
8+
cnt.set(w, (cnt.get(w) ?? 0) + 1);
9+
}
10+
}
11+
}
12+
let ans = responses[0][0];
13+
for (const [w, v] of cnt) {
14+
const best = cnt.get(ans)!;
15+
if (best < v || (best === v && w < ans)) {
16+
ans = w;
17+
}
18+
}
19+
return ans;
20+
}

0 commit comments

Comments
 (0)