array and hashmap September 12, 2022

Sender with largest word count

Time O(nlog(n)) Space O(n) Open original problem

We will create a count each words of the the messages for each sender. Then we take the sender with the highest number of word count, sort them and then return the largest sender name lexicographically.

class Solution:
    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:
        count = collections.Counter()
        for message, sender in zip(messages, senders):
            count[sender] += len(message.split())

        maxVal = max(count.values())
        res = []
        for sender, cnt in count.items():
            if cnt == maxVal:
                res.append(sender)

        return sorted(res)[-1]

Time Complexity: O(nlog(n))
Space Complexity: O(n)