Skip to content

Commit 29d4ab2

Browse files
committed
feat: add solutions to lc problem: No.2284
No.2284.Sender With Largest Word Count
1 parent ec2dfa0 commit 29d4ab2

File tree

6 files changed

+93
-66
lines changed

6 files changed

+93
-66
lines changed

solution/2200-2299/2284.Sender With Largest Word Count/README.md

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ Charlie 总共发出了 5 个单词。
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:哈希表 + 枚举**
62+
63+
我们用哈希表 `cnt` 统计每个发件人的单词数,然后枚举每个发件人,找到单词数最多且字典序最大的发件人即可。
64+
65+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `messages` 的长度。
66+
6167
<!-- tabs:start -->
6268

6369
### **Python3**
@@ -68,9 +74,13 @@ Charlie 总共发出了 5 个单词。
6874
class Solution:
6975
def largestWordCount(self, messages: List[str], senders: List[str]) -> str:
7076
cnt = Counter()
71-
for m, s in zip(messages, senders):
72-
cnt[s] += m.count(' ') + 1
73-
return sorted(cnt.items(), key=lambda x: (x[1], x[0]))[-1][0]
77+
for msg, sender in zip(messages, senders):
78+
cnt[sender] += msg.count(' ') + 1
79+
ans = ''
80+
for sender, v in cnt.items():
81+
if cnt[ans] < v or (cnt[ans] == v and ans < sender):
82+
ans = sender
83+
return ans
7484
```
7585

7686
### **Java**
@@ -83,14 +93,19 @@ class Solution {
8393
Map<String, Integer> cnt = new HashMap<>();
8494
int n = senders.length;
8595
for (int i = 0; i < n; ++i) {
86-
cnt.put(senders[i], cnt.getOrDefault(senders[i], 0) + messages[i].split(" ").length);
96+
int v = 1;
97+
for (int j = 0; j < messages[i].length(); ++j) {
98+
if (messages[i].charAt(j) == ' ') {
99+
++v;
100+
}
101+
}
102+
cnt.merge(senders[i], v, Integer::sum);
87103
}
88104
String ans = senders[0];
89-
for (Map.Entry<String, Integer> e : cnt.entrySet()) {
90-
String u = e.getKey();
91-
int v = e.getValue();
92-
if (v > cnt.get(ans) || (v == cnt.get(ans) && ans.compareTo(u) < 0)) {
93-
ans = u;
105+
for (var e : cnt.entrySet()) {
106+
String sender = e.getKey();
107+
if (cnt.get(ans) < cnt.get(sender) || (cnt.get(ans) == cnt.get(sender) && ans.compareTo(sender) < 0)) {
108+
ans = sender;
94109
}
95110
}
96111
return ans;
@@ -107,15 +122,14 @@ public:
107122
unordered_map<string, int> cnt;
108123
int n = senders.size();
109124
for (int i = 0; i < n; ++i) {
110-
int v = 0;
111-
for (char& c : messages[i]) {
112-
if (c == ' ') ++v;
113-
}
114-
cnt[senders[i]] += v + 1;
125+
int v = count(messages[i].begin(), messages[i].end(), ' ') + 1;
126+
cnt[senders[i]] += v;
115127
}
116128
string ans = senders[0];
117-
for (auto& [u, v] : cnt) {
118-
if (v > cnt[ans] || (v == cnt[ans] && u > ans)) ans = u;
129+
for (auto& [sender, v] : cnt) {
130+
if (cnt[ans] < v || (cnt[ans] == v && ans < sender)) {
131+
ans = sender;
132+
}
119133
}
120134
return ans;
121135
}
@@ -125,19 +139,18 @@ public:
125139
### **Go**
126140
127141
```go
128-
func largestWordCount(messages []string, senders []string) string {
142+
func largestWordCount(messages []string, senders []string) (ans string) {
129143
cnt := map[string]int{}
130144
for i, msg := range messages {
131145
v := strings.Count(msg, " ") + 1
132146
cnt[senders[i]] += v
133147
}
134-
ans := ""
135-
for u, v := range cnt {
136-
if v > cnt[ans] || (v == cnt[ans] && u > ans) {
137-
ans = u
148+
for sender, v := range cnt {
149+
if cnt[ans] < v || (cnt[ans] == v && ans < sender) {
150+
ans = sender
138151
}
139152
}
140-
return ans
153+
return
141154
}
142155
```
143156

solution/2200-2299/2284.Sender With Largest Word Count/README_EN.md

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ Since there is a tie for the largest word count, we return the sender with the l
6262
class Solution:
6363
def largestWordCount(self, messages: List[str], senders: List[str]) -> str:
6464
cnt = Counter()
65-
for m, s in zip(messages, senders):
66-
cnt[s] += m.count(' ') + 1
67-
return sorted(cnt.items(), key=lambda x: (x[1], x[0]))[-1][0]
65+
for msg, sender in zip(messages, senders):
66+
cnt[sender] += msg.count(' ') + 1
67+
ans = ''
68+
for sender, v in cnt.items():
69+
if cnt[ans] < v or (cnt[ans] == v and ans < sender):
70+
ans = sender
71+
return ans
6872
```
6973

7074
### **Java**
@@ -75,14 +79,19 @@ class Solution {
7579
Map<String, Integer> cnt = new HashMap<>();
7680
int n = senders.length;
7781
for (int i = 0; i < n; ++i) {
78-
cnt.put(senders[i], cnt.getOrDefault(senders[i], 0) + messages[i].split(" ").length);
82+
int v = 1;
83+
for (int j = 0; j < messages[i].length(); ++j) {
84+
if (messages[i].charAt(j) == ' ') {
85+
++v;
86+
}
87+
}
88+
cnt.merge(senders[i], v, Integer::sum);
7989
}
8090
String ans = senders[0];
81-
for (Map.Entry<String, Integer> e : cnt.entrySet()) {
82-
String u = e.getKey();
83-
int v = e.getValue();
84-
if (v > cnt.get(ans) || (v == cnt.get(ans) && ans.compareTo(u) < 0)) {
85-
ans = u;
91+
for (var e : cnt.entrySet()) {
92+
String sender = e.getKey();
93+
if (cnt.get(ans) < cnt.get(sender) || (cnt.get(ans) == cnt.get(sender) && ans.compareTo(sender) < 0)) {
94+
ans = sender;
8695
}
8796
}
8897
return ans;
@@ -99,15 +108,14 @@ public:
99108
unordered_map<string, int> cnt;
100109
int n = senders.size();
101110
for (int i = 0; i < n; ++i) {
102-
int v = 0;
103-
for (char& c : messages[i]) {
104-
if (c == ' ') ++v;
105-
}
106-
cnt[senders[i]] += v + 1;
111+
int v = count(messages[i].begin(), messages[i].end(), ' ') + 1;
112+
cnt[senders[i]] += v;
107113
}
108114
string ans = senders[0];
109-
for (auto& [u, v] : cnt) {
110-
if (v > cnt[ans] || (v == cnt[ans] && u > ans)) ans = u;
115+
for (auto& [sender, v] : cnt) {
116+
if (cnt[ans] < v || (cnt[ans] == v && ans < sender)) {
117+
ans = sender;
118+
}
111119
}
112120
return ans;
113121
}
@@ -117,19 +125,18 @@ public:
117125
### **Go**
118126
119127
```go
120-
func largestWordCount(messages []string, senders []string) string {
128+
func largestWordCount(messages []string, senders []string) (ans string) {
121129
cnt := map[string]int{}
122130
for i, msg := range messages {
123131
v := strings.Count(msg, " ") + 1
124132
cnt[senders[i]] += v
125133
}
126-
ans := ""
127-
for u, v := range cnt {
128-
if v > cnt[ans] || (v == cnt[ans] && u > ans) {
129-
ans = u
134+
for sender, v := range cnt {
135+
if cnt[ans] < v || (cnt[ans] == v && ans < sender) {
136+
ans = sender
130137
}
131138
}
132-
return ans
139+
return
133140
}
134141
```
135142

solution/2200-2299/2284.Sender With Largest Word Count/Solution.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ class Solution {
44
unordered_map<string, int> cnt;
55
int n = senders.size();
66
for (int i = 0; i < n; ++i) {
7-
int v = 0;
8-
for (char& c : messages[i]) {
9-
if (c == ' ') ++v;
10-
}
11-
cnt[senders[i]] += v + 1;
7+
int v = count(messages[i].begin(), messages[i].end(), ' ') + 1;
8+
cnt[senders[i]] += v;
129
}
1310
string ans = senders[0];
14-
for (auto& [u, v] : cnt) {
15-
if (v > cnt[ans] || (v == cnt[ans] && u > ans)) ans = u;
11+
for (auto& [sender, v] : cnt) {
12+
if (cnt[ans] < v || (cnt[ans] == v && ans < sender)) {
13+
ans = sender;
14+
}
1615
}
1716
return ans;
1817
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
func largestWordCount(messages []string, senders []string) string {
1+
func largestWordCount(messages []string, senders []string) (ans string) {
22
cnt := map[string]int{}
33
for i, msg := range messages {
44
v := strings.Count(msg, " ") + 1
55
cnt[senders[i]] += v
66
}
7-
ans := ""
8-
for u, v := range cnt {
9-
if v > cnt[ans] || (v == cnt[ans] && u > ans) {
10-
ans = u
7+
for sender, v := range cnt {
8+
if cnt[ans] < v || (cnt[ans] == v && ans < sender) {
9+
ans = sender
1110
}
1211
}
13-
return ans
12+
return
1413
}

solution/2200-2299/2284.Sender With Largest Word Count/Solution.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ public String largestWordCount(String[] messages, String[] senders) {
33
Map<String, Integer> cnt = new HashMap<>();
44
int n = senders.length;
55
for (int i = 0; i < n; ++i) {
6-
cnt.put(senders[i], cnt.getOrDefault(senders[i], 0) + messages[i].split(" ").length);
6+
int v = 1;
7+
for (int j = 0; j < messages[i].length(); ++j) {
8+
if (messages[i].charAt(j) == ' ') {
9+
++v;
10+
}
11+
}
12+
cnt.merge(senders[i], v, Integer::sum);
713
}
814
String ans = senders[0];
9-
for (Map.Entry<String, Integer> e : cnt.entrySet()) {
10-
String u = e.getKey();
11-
int v = e.getValue();
12-
if (v > cnt.get(ans) || (v == cnt.get(ans) && ans.compareTo(u) < 0)) {
13-
ans = u;
15+
for (var e : cnt.entrySet()) {
16+
String sender = e.getKey();
17+
if (cnt.get(ans) < cnt.get(sender) || (cnt.get(ans) == cnt.get(sender) && ans.compareTo(sender) < 0)) {
18+
ans = sender;
1419
}
1520
}
1621
return ans;
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class Solution:
22
def largestWordCount(self, messages: List[str], senders: List[str]) -> str:
33
cnt = Counter()
4-
for m, s in zip(messages, senders):
5-
cnt[s] += m.count(' ') + 1
6-
return sorted(cnt.items(), key=lambda x: (x[1], x[0]))[-1][0]
4+
for msg, sender in zip(messages, senders):
5+
cnt[sender] += msg.count(' ') + 1
6+
ans = ''
7+
for sender, v in cnt.items():
8+
if cnt[ans] < v or (cnt[ans] == v and ans < sender):
9+
ans = sender
10+
return ans

0 commit comments

Comments
 (0)