[LeetCode] Sender With Largest Word Count

2284. Sender With Largest Word Count

You have a chat log of n messages. You are given two string arrays messages and senders where messages[i] is a message sent by senders[i].

A message is list of words that are separated by a single space with no leading or trailing spaces. The word count of a sender is the total number of words sent by the sender. Note that a sender may send more than one message.

Return the sender with the largest word count. If there is more than one sender with the largest word count, return the one with the lexicographically largest name.

Note:

  • Uppercase letters come before lowercase letters in lexicographical order.
  • “Alice” and “alice” are distinct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
int count(string& s) {
int res = 1;
for(auto ch : s)
if(ch == ' ')res++;
return res;
}
public:
string largestWordCount(vector<string>& messages, vector<string>& senders) {
unordered_map<string, int> counter;
int n = messages.size();
for(int i = 0; i < n; i++) {
counter[senders[i]] += count(messages[i]);
}
int ma = -1;
string res = "";
for(auto& [name, count] : counter) {
if(count > ma) {
res = name;
ma = count;
} else if(count == ma) res = max(res, name);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/29/PS/LeetCode/sender-with-largest-word-count/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.