Sender With Largest Word Count — Problem & Solution
Sender With Largest Word Count solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.
Sender With Largest Word Count is a medium coding problem on arrays, strings and hash table, asked in Amazon interviews.
- Difficulty: Medium
- Topics: Arrays, Strings, Hash Table, Counting
- Asked at: Amazon
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You have a chat log of n messages: messages[i] was sent by senders[i]. Each message is a list of words separated by single spaces, with no leading or trailing spaces.
A sender's word count is the total number of words across all the messages they sent. Return the sender with the largest word count. If several senders tie, return the one whose name is lexicographically largest — comparing characters by their codes, so every uppercase letter sorts before every lowercase letter, and names differing in case are different senders.
Example 1
Input: messages = ["ship it","review the patch please","lgtm"], senders = ["dev","Ana","dev"]
Output: Ana
Explanation: `dev` sent 2 + 1 = 3 words; `Ana` sent 4.
Example 2
Input: messages = ["a b c","d e f"], senders = ["Zoe","amy"]
Output: amy
Explanation: Both sent 3 words. `amy` is larger than `Zoe` because lowercase `a` comes after uppercase `Z`.
Example 3
Input: messages = ["hi"], senders = ["Sam"]
Output: Sam
Constraints
n == messages.length == senders.length1 <= n <= 10^41 <= messages[i].length <= 1001 <= senders[i].length <= 10messages[i] holds English letters and single spaces between wordssenders[i] holds English letters only
Hints for Sender With Largest Word Count
Hint 1
The number of words in a message is the number of spaces plus one.
Hint 2
Accumulate a total per sender in a hash map.
Hint 3
Scan the map for the largest total, breaking ties with a plain string comparison (
>on the names).
How to solve Sender With Largest Word Count
Sum word counts per sender in a hash map, then pick the maximum with ties going to the lexicographically larger name.
Approach
- For each message, count its words as
spaces + 1and add that tototal[senders[i]]. - Walk the map, keeping the sender with the larger total, or the larger name when totals are equal.
- Return that sender.
Why it works
Words are separated by exactly one space and the message has no outer spaces, so the space count is one less than the word count. The final scan compares every sender on the pair (total, name), which is exactly the required order.
Complexity
- Time —
O(total message length) - Space —
O(number of senders)
Pitfalls
- Counting messages instead of words.
- Case-insensitive comparison:
amyandAmyare different senders, and the ordering is by character code. - Ties need the largest name, not the first seen.
Reference solution
Python
from typing import List
def largestWordCount(messages: List[str], senders: List[str]) -> str:
total = {}
for m, s in zip(messages, senders):
total[s] = total.get(s, 0) + m.count(' ') + 1
return max(total.items(), key=lambda e: (e[1], e[0]))[0]Java
public static String largestWordCount(String[] messages, String[] senders) {
Map<String, Integer> total = new HashMap<>();
for (int i = 0; i < messages.length; i++) {
int words = 1;
for (int j = 0; j < messages[i].length(); j++) if (messages[i].charAt(j) == ' ') words++;
total.merge(senders[i], words, Integer::sum);
}
String best = "";
int bestC = -1;
for (Map.Entry<String, Integer> e : total.entrySet()) {
int c = e.getValue();
if (c > bestC || (c == bestC && e.getKey().compareTo(best) > 0)) { best = e.getKey(); bestC = c; }
}
return best;
}C++
string largestWordCount(vector<string>& messages, vector<string>& senders) {
unordered_map<string, int> total;
for (size_t i = 0; i < messages.size(); i++)
total[senders[i]] += (int)count(messages[i].begin(), messages[i].end(), ' ') + 1;
string best;
int bestC = -1;
for (auto& e : total) {
if (e.second > bestC || (e.second == bestC && e.first > best)) { best = e.first; bestC = e.second; }
}
return best;
}JavaScript
var largestWordCount = function(messages, senders) {
var total = new Map();
for (var i = 0; i < messages.length; i++) {
var words = messages[i].split(' ').length;
total.set(senders[i], (total.get(senders[i]) || 0) + words);
}
var best = '', bestC = -1;
total.forEach(function(c, s) {
if (c > bestC || (c === bestC && s > best)) { best = s; bestC = c; }
});
return best;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.