Sender With Largest Word Count - Problem

Imagine you're analyzing a busy chat room where multiple users are having conversations! ๐Ÿ’ฌ

You have a chat log containing n messages. You're given two string arrays: messages and senders, where messages[i] is a message sent by senders[i].

Your task: Find the sender who has sent the most words across all their messages!

Key details:

  • Each message is a list of words separated by single spaces (no leading/trailing spaces)
  • A sender's word count is the total number of words across all their messages
  • If there's a tie, return the sender with the lexicographically largest name
  • Remember: Uppercase letters come before lowercase in lexicographical order, so "Alice" โ‰  "alice"

Example: If Alice sends "Hello world" and "Good morning", her word count is 4 words total.

Input & Output

example_1.py โ€” Basic Case
$ Input: messages = ["Hello userTwooo", "Hi userThree", "Wonderful day Alice", "Nice day userThree"] senders = ["Alice", "Bob", "Alice", "Bob"]
โ€บ Output: "Alice"
๐Ÿ’ก Note: Alice sent "Hello userTwooo" (2 words) and "Wonderful day Alice" (3 words) = 5 total words. Bob sent "Hi userThree" (2 words) and "Nice day userThree" (3 words) = 5 total words. Since both have 5 words, we return "Alice" because "Alice" > "Bob" lexicographically.
example_2.py โ€” Clear Winner
$ Input: messages = ["How is leetcode for everyone", "Leetcode is useful for practice"] senders = ["Bob", "Charlie"]
โ€บ Output: "Charlie"
๐Ÿ’ก Note: Bob sent 5 words, Charlie sent 5 words. Since it's a tie, we compare lexicographically: "Charlie" > "Bob", so Charlie wins.
example_3.py โ€” Single Word Messages
$ Input: messages = ["Hello", "Hi", "Hey"] senders = ["Alice", "Alice", "Bob"]
โ€บ Output: "Alice"
๐Ÿ’ก Note: Alice sent 2 messages with 1 word each = 2 total words. Bob sent 1 message with 1 word = 1 total word. Alice has more words, so she wins.

Visualization

Tap to expand
๐Ÿ† Chat Room Word Count Championship๐Ÿ“ฑ Chat MessagesAlice: "Hello everyone good morning"๐Ÿ‘† 4 wordsBob: "Hi there"๐Ÿ‘† 2 wordsAlice: "How is everyone today"๐Ÿ‘† 4 wordsBob: "Great thanks for asking buddy"๐Ÿ‘† 5 words๐Ÿ† Live Scoreboard๐Ÿ‘‘ Alice: 8 words(4 + 4 from her messages)Bob: 7 words(2 + 5 from his messages)๐ŸŽ‰ Winner: Alice!โšก One-Pass Algorithm1. Read each message โ†’ 2. Count words & update sender total โ†’ 3. Check if new champion โ†’ 4. Handle ties lexicographically
Understanding the Visualization
1
Read Messages
Process each chat message one by one, identifying sender and counting words
2
Update Scoreboard
Add word count to sender's running total in our hash table scoreboard
3
Track Leader
After each update, check if current sender is the new champion
4
Handle Ties
When word counts are equal, the lexicographically larger name wins
Key Takeaway
๐ŸŽฏ Key Insight: By processing messages in a single pass and maintaining both the word count hash map and current winner simultaneously, we achieve optimal O(n) time complexity while handling lexicographical tie-breaking elegantly.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

Single pass through n messages, plus m total words to count across all messages

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Hash map space for k unique senders, typically much smaller than n

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 104 where n is the number of messages
  • 1 โ‰ค messages[i].length, senders[i].length โ‰ค 100
  • messages[i] consists of lowercase English letters and spaces
  • senders[i] consists of uppercase and lowercase English letters
  • All messages[i] are non-empty and do not have leading or trailing spaces
  • All words in messages[i] are separated by a single space
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
28.3K Views
Medium Frequency
~15 min Avg. Time
847 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