Sender With Largest Word Count - Problem

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 a 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.

Input & Output

Example 1 — Basic Case
$ Input: messages = ["Hello userTwooo", "Hi userThree", "Wonderful day Alice", "Nice day userThree"], senders = ["Alice", "Bob", "Alice", "Bob"]
Output: Alice
💡 Note: Alice sends "Hello userTwooo" (2 words) + "Wonderful day Alice" (3 words) = 5 words. Bob sends "Hi userThree" (2 words) + "Nice day userThree" (3 words) = 5 words. Since both have 5 words, return lexicographically largest: "Alice" > "Bob", so Alice wins.
Example 2 — Clear Winner
$ Input: messages = ["How is leetcode for everyone", "Leetcode is useful for practice"], senders = ["Bob", "Alice"]
Output: Bob
💡 Note: Bob sends 5 words, Alice sends 5 words. Both tied at 5 words. "Bob" > "Alice" lexicographically, so Bob wins.
Example 3 — Multiple Messages
$ Input: messages = ["a", "bb", "ccc"], senders = ["Alice", "Alice", "Bob"]
Output: Alice
💡 Note: Alice sends "a" (1 word) + "bb" (1 word) = 2 words. Bob sends "ccc" (1 word) = 1 word. Alice has more words: 2 > 1.

Constraints

  • 1 ≤ messages.length ≤ 104
  • 1 ≤ messages[i].length ≤ 100
  • 1 ≤ senders[i].length ≤ 20
  • messages[i] consists of lowercase English letters and ' ' (space)
  • 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

Visualization

Tap to expand
Sender With Largest Word CountMessages["Hello world", "How are you", "Great"]Senders["Alice", "Bob", "Alice"]Word Count MapAlice: 2 + 1 = 3Bob: 3Winner: Alice (3 words)
Understanding the Visualization
1
Input
Array of messages and corresponding senders
2
Process
Count words per sender using hash map
3
Output
Return sender with maximum word count (lexicographic tiebreaker)
Key Takeaway
🎯 Key Insight: Use hash map to accumulate word counts per sender, then find maximum with lexicographic tiebreaker
Asked in
Meta 12 Amazon 8 Google 6
12.5K Views
Medium Frequency
~15 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen