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 thelargest word count. If there is more than one sender with the largest word count, return the one with thelexicographically largest name.
Note:
Uppercase letters come before lowercase letters in lexicographical order.
Input: messages =["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders =["Alice","userTwo","userThree","Alice"]Output: "Alice"Explanation: Alice sends a total of 2+3=5 words.userTwo sends a total of 2 words.userThree sends a total of 3 words.Since Alice has the largest word count, we return"Alice".
Input: messages =["How is leetcode for everyone","Leetcode is useful for practice"], senders =["Bob","Charlie"]Output: "Charlie"Explanation: Bob sends a total of 5 words.Charlie sends a total of 5 words.Since there is a tie for the largest word count, we return the sender with the lexicographically larger name, Charlie.
Count the total number of words sent by each sender. Then, return the sender with the largest word count, breaking ties by lexicographically largest name.
classSolution {
public: string largestWordCount(vector<string>& messages, vector<string>& senders) {
unordered_map<string, int> cnt;
int n = messages.size();
for (int i =0; i < n; ++i) {
int words =1;
for (char c : messages[i]) if (c ==' ') ++words;
cnt[senders[i]] += words;
}
string ans;
int mx =0;
for (auto& [k, v] : cnt) {
if (v > mx || (v == mx && k > ans)) {
mx = v;
ans = k;
}
}
return ans;
}
};
import java.util.*;
classSolution {
public String largestWordCount(String[] messages, String[] senders) {
Map<String, Integer> cnt =new HashMap<>();
for (int i = 0; i < messages.length; ++i) {
int words = messages[i].split(" ").length;
cnt.put(senders[i], cnt.getOrDefault(senders[i], 0) + words);
}
String ans ="";
int mx = 0;
for (String k : cnt.keySet()) {
int v = cnt.get(k);
if (v > mx || (v == mx && k.compareTo(ans) > 0)) {
mx = v;
ans = k;
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funlargestWordCount(messages: Array<String>, senders: Array<String>): String {
val cnt = mutableMapOf<String, Int>()
for (i in messages.indices) {
val words = messages[i].split(" ").size
cnt[senders[i]] = cnt.getOrDefault(senders[i], 0) + words
}
var ans = ""var mx = 0for ((k, v) in cnt) {
if (v > mx || (v == mx && k > ans)) {
mx = v
ans = k
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
deflargestWordCount(self, messages: list[str], senders: list[str]) -> str:
cnt = {}
for msg, snd in zip(messages, senders):
words = msg.count(' ') +1 cnt[snd] = cnt.get(snd, 0) + words
mx =0 ans =''for k, v in cnt.items():
if v > mx or (v == mx and k > ans):
mx = v
ans = k
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use std::collections::HashMap;
impl Solution {
pubfnlargest_word_count(messages: Vec<String>, senders: Vec<String>) -> String {
letmut cnt = HashMap::new();
for (msg, snd) in messages.iter().zip(senders.iter()) {
let words = msg.chars().filter(|&c| c ==' ').count() +1;
*cnt.entry(snd).or_insert(0) += words;
}
letmut ans ="".to_string();
letmut mx =0;
for (k, &v) in&cnt {
if v > mx || (v == mx && k >&ans) {
mx = v;
ans = k.clone();
}
}
ans
}
}