2284. Sender With Largest Word Count

problem

solution

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
26
class Solution {
public:
int split(string strs, char delimiters){
int size = 0;
for(char c:strs){
if(c==delimiters) size++;
}
return ++size;
}
string largestWordCount(vector<string>& messages, vector<string>& senders) {
map<string, int> mp; // If there is more than one sender with the largest word count, return the one with the lexicographically largest name.
int n = senders.size();
for(int i=0;i<n;++i){
mp[senders[i]]+=split(messages[i], ' ');
}
string ret;
int count =0;
for(auto &[k,v]:mp){
if(v>=count) {
ret = k;
count = v;
}
}
return ret;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
string largestWordCount(vector<string>& messages, vector<string>& senders) {
unordered_map<string, int> mp; // If there is more than one sender with the largest word count, return the one with the lexicographically largest name.
int n = senders.size();
string ret;
int count =0;
for(int i=0;i<n;++i){
int words = std::count(begin(messages[i]), end(messages[i]), ' ') + 1;
int freq = mp[senders[i]] += words;
if(freq > count || ( freq == count && senders[i]>ret ) ) {
ret = senders[i];
count = freq;
}
}
return ret;
}
};