LeetCode 2284: Sender With Largest Word Count Solution

Master LeetCode problem 2284 (Sender With Largest Word Count), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2284. Sender With Largest Word Count

Problem Explanation

Explanation

To solve this problem, we can iterate through the messages and keep track of the word count for each sender using a HashMap. After calculating the word count for each sender, we can then find the sender with the largest word count. In case of a tie, we return the sender with the lexicographically largest name.

  • Create a HashMap to store the word count for each sender.
  • Iterate through the messages array and split each message into words.
  • Update the word count for the sender of the current message.
  • Find the sender with the largest word count. If there is a tie, return the sender with the lexicographically largest name.

Solution Code

import java.util.*;

class Solution {
    public String largestSender(String[] messages, String[] senders) {
        Map<String, Integer> wordCount = new HashMap<>();
        
        for (int i = 0; i < messages.length; i++) {
            String sender = senders[i];
            String[] words = messages[i].split(" ");
            wordCount.put(sender, wordCount.getOrDefault(sender, 0) + words.length);
        }
        
        String largestSender = "";
        int maxCount = 0;
        
        for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
            if (entry.getValue() > maxCount || (entry.getValue() == maxCount && entry.getKey().compareTo(largestSender) > 0)) {
                maxCount = entry.getValue();
                largestSender = entry.getKey();
            }
        }
        
        return largestSender;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2284 (Sender With Largest Word Count)?

This page provides optimized solutions for LeetCode problem 2284 (Sender With Largest Word Count) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2284 (Sender With Largest Word Count)?

The time complexity for LeetCode 2284 (Sender With Largest Word Count) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2284 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2284 in Java, C++, or Python.

Back to LeetCode Solutions