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
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
โ Linear Growth
Space Complexity
O(k)
Hash map space for k unique senders, typically much smaller than n
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code