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:
"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 ' '
.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.This problem requires finding the sender with the largest word count in a series of messages. If there's a tie, the lexicographically largest sender name should be returned.
Approach:
The most efficient approach uses a hash table (dictionary in Python) to store the word count for each sender. We iterate through the messages
and senders
arrays simultaneously. For each message, we count the words (by counting spaces plus one) and add it to the sender's count in the hash table. After processing all messages, we iterate through the hash table to find the sender with the maximum word count. If multiple senders have the same maximum count, we select the one with the lexicographically larger name.
Time Complexity Analysis:
count()
in C++, split()
in TypeScript, or strings.Count()
in Go), the time complexity becomes O(L) in total as we can count spaces efficiently.Space Complexity Analysis:
The space complexity is determined by the size of the hash table. In the worst case, each sender has a unique entry, leading to O(n) space complexity where n is the number of senders. This is because we store the word counts for each sender.
Code Implementation (Python):
from collections import Counter
class Solution:
def largestWordCount(self, messages: List[str], senders: List[str]) -> str:
word_counts = Counter() #Efficient counter for word counts
for message, sender in zip(messages, senders):
word_counts[sender] += message.count(" ") + 1 #Efficient word count
max_count = 0
result_sender = ""
for sender, count in word_counts.items():
if count > max_count:
max_count = count
result_sender = sender
elif count == max_count and sender > result_sender: #Lexicographical comparison
result_sender = sender
return result_sender
The code in other languages (Java, C++, Go, TypeScript) follow the same logic, only differing in syntax and data structures. The optimized versions using built-in functions for counting words significantly reduce the time complexity from O(nL) to O(L) for word counting. The overall complexity is then O(L) where L is the total number of characters in the messages plus O(n) for maintaining the word counts in the hash table. Therefore, overall, the time complexity is O(L) dominated by the word counting. The space complexity remains O(n).