We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Sender With Largest Word Count

Number: 2378

Difficulty: Medium

Paid? No

Companies: Google


Problem Description

Given two arrays, messages and senders, where messages[i] is a message sent by senders[i] and each message consists of words separated by single spaces, the task is to determine the sender with the largest total word count. In case of a tie (i.e., multiple senders have the same maximum word count), the sender with the lexicographically largest name should be returned. Note that uppercase letters are considered lexicographically smaller than lowercase.


Key Insights

  • Count the number of words in each message by splitting on spaces.
  • Accumulate the total word count for each sender using a hash table (or dictionary).
  • After counting, iterate through the hash table to find the sender with the maximum word count.
  • If two senders have the same word count, compare their names lexicographically.

Space and Time Complexity

Time Complexity: O(n * m) where n is the number of messages and m is the average length (in words) of a message. Space Complexity: O(u) where u is the number of unique senders.


Solution

The approach starts by initializing a hash table to keep track of word counts for each sender. For every message, split the string on spaces to get the word count and add this count to the respective sender's total in the hash table. Once all messages have been processed, iterate through the hash table to determine the sender with the highest word count. If a tie occurs, take the sender with the lexicographically larger name. Key data structures used include dictionaries (hash tables) for accumulating counts and strings for message processing.


Code Solutions

# Python solution

# Define a function to get the sender with the largest word count
def largestWordCount(messages, senders):
    # Dictionary to store total word count for each sender
    sender_word_count = {}
    
    # Loop through each message and sender simultaneously
    for message, sender in zip(messages, senders):
        # Count words in message by splitting the string
        word_count = len(message.split())
        # Add the word count to the sender's total in the dictionary.
        sender_word_count[sender] = sender_word_count.get(sender, 0) + word_count
    
    # Initialize variables to track the best sender and maximum count
    max_sender = ""
    max_count = 0

    # Iterate over the dictionary items
    for sender, count in sender_word_count.items():
        # Check if current count is greater than max_count, or if count is the same but sender is lexicographically larger
        if count > max_count or (count == max_count and sender > max_sender):
            max_count = count
            max_sender = sender

    return max_sender

# Example usage:
messages = ["Hello userTwooo", "Hi userThree", "Wonderful day Alice", "Nice day userThree"]
senders = ["Alice", "userTwo", "userThree", "Alice"]
print(largestWordCount(messages, senders))  # Output: "Alice"
← Back to All Questions