2284. Sender With Largest Word Count LeetCode Solution
In this guide, you will get 2284. Sender With Largest Word Count LeetCode Solution with the best time and space complexity. The solution to Sender With Largest Word Count problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Sender With Largest Word Count solution in C++
- Sender With Largest Word Count solution in Java
- Sender With Largest Word Count solution in Python
- Additional Resources

Problem Statement of 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.
Example 1:
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”.
Example 2:
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.
Constraints:
n == messages.length == senders.length
1 <= n <= 104
1 <= messages[i].length <= 100
1 <= senders[i].length <= 10
messages[i] consists of uppercase and lowercase English letters and ' '.
All the words in messages[i] are separated by a single space.
messages[i] does not have leading or trailing spaces.
senders[i] consists of uppercase and lowercase English letters only.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2284. Sender With Largest Word Count LeetCode Solution in C++
class Solution {
public:
string largestWordCount(vector<string>& messages, vector<string>& senders) {
const int n = messages.size();
string ans;
int maxWordsSent = 0;
unordered_map<string, int> count; // {sender, the number of words sent}
for (int i = 0; i < n; ++i) {
const string& message = messages[i];
const string& sender = senders[i];
const int wordsCount = ranges::count(message, ' ') + 1;
count[sender] += wordsCount;
const int numWordsSent = count[sender];
if (numWordsSent > maxWordsSent) {
ans = sender;
maxWordsSent = numWordsSent;
} else if (numWordsSent == maxWordsSent && sender > ans) {
ans = sender;
}
}
return ans;
}
};
/* code provided by PROGIEZ */
2284. Sender With Largest Word Count LeetCode Solution in Java
class Solution {
public String largestWordCount(String[] messages, String[] senders) {
final int n = messages.length;
String ans = "";
int maxWordsSent = 0;
Map<String, Integer> count = new HashMap<>(); // {sender, the number of words sent}
for (int i = 0; i < n; ++i) {
final String message = messages[i];
final String sender = senders[i];
final int wordsCount = (int) message.chars().filter(c -> c == ' ').count() + 1;
final int numWordsSent = count.merge(sender, wordsCount, Integer::sum);
if (numWordsSent > maxWordsSent) {
ans = sender;
maxWordsSent = numWordsSent;
} else if (numWordsSent == maxWordsSent && sender.compareTo(ans) > 0) {
ans = sender;
}
}
return ans;
}
}
// code provided by PROGIEZ
2284. Sender With Largest Word Count LeetCode Solution in Python
class Solution:
def largestWordCount(self, messages: list[str], senders: list[str]) -> str:
n = len(messages)
ans = ''
maxWordsSent = 0
count = collections.Counter() # [sender, # Words sent]
for message, sender in zip(messages, senders):
wordsCount = message.count(' ') + 1
count[sender] += wordsCount
numWordsSent = count[sender]
if numWordsSent > maxWordsSent:
ans = sender
maxWordsSent = numWordsSent
elif numWordsSent == maxWordsSent and sender > ans:
ans = sender
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.