Skip to main content

Sender With Largest Word Count - Solution & Explanation

MediumArrayHash TableStringCounting17 min readAsked at: Google
Practice this problem

Problem Statement

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:

  • Uppercase letters come before lowercase letters in lexicographical order.
  • "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 ' '.
  • All the words in 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.

Approach Overview

Problem Overview: You receive two arrays: messages and senders. Each message belongs to the sender at the same index. The goal is to compute the total number of words sent by each sender and return the sender with the largest cumulative word count. If multiple senders tie, return the lexicographically largest sender name.

Approach 1: Divide and Conquer (O(n·m) time, O(k) space)

Split the messages array into smaller segments and compute word counts for each segment independently. Each subproblem counts words in its portion and aggregates totals in a hash map keyed by sender. During the merge step, combine maps by summing counts for identical senders. Word counting per message can be done by counting spaces and adding one, or by splitting the string. The final pass scans the aggregated map to find the sender with the maximum total, breaking ties using lexicographic comparison.

This approach mirrors classic divide and conquer patterns where large datasets are processed in chunks. It works well when message data is distributed or processed in parallel systems. The dominant cost is iterating through all characters of all messages, giving O(n·m) time where m is average message length, and O(k) space for storing word counts of k unique senders.

Approach 2: Dynamic Programming / Incremental Counting (O(n·m) time, O(k) space)

Process messages sequentially while maintaining a running total of words per sender. Use a hash table where the key is the sender name and the value is the cumulative word count. For each message, compute its word count and immediately update the sender’s total. While updating, track the current best sender by comparing totals and resolving ties using lexicographic order.

This incremental strategy behaves like a lightweight dynamic programming pattern: each state (sender total) builds on previous results instead of recomputing counts. Because every message is processed exactly once and each update is O(1) average time, the full algorithm runs in O(n·m). Space usage remains O(k) for storing totals of unique senders.

Recommended for interviews: The hash table counting approach (Approach 2) is what interviewers expect. It shows you recognize the problem as a frequency aggregation task using HashMap/dict. Mentioning the divide-and-conquer variation demonstrates awareness of scalable data processing, but the single-pass counting solution is the most direct and practical.

Approach 1: Approach 1: Divide and Conquer

This approach leverages the divide and conquer strategy to break down the problem into smaller, more manageable sub-problems. The basic idea is to recursively divide the problem into two or more sub-problems until they become simple enough to solve directly. The results of the sub-problems are then combined to give a solution to the original problem.

This solution uses the merge sort algorithm, a classic example of divide and conquer. The array is recursively divided until each sub-array has one element. Then, it conquers (merges) the divided arrays back together in sorted order.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n) - The algorithm splits the array into halves log n times, and for each half, it performs a linear merge.
Space Complexity: O(n) - Temporary arrays are used to store sorted sub-elements.

Try this approach in the editor →

Approach 2: Approach 2: Dynamic Programming

This approach uses dynamic programming to build up a solution incrementally. Dynamic programming is especially useful in optimization problems where sub-problems overlap. By storing the results of sub-problems, we avoid redundant computations, thus optimizing the solution process.

This C code calculates the Fibonacci sequence up to the nth term using dynamic programming. By storing the results of sub-problems in dp[], the solution becomes more efficient than a naive recursive approach.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) - Each Fibonacci term is computed only once.
Space Complexity: O(n) - Extra memory is used to store solutions to sub-problems.

Try this approach in the editor →

Approach 3: Hash Table + Enumeration

We can use a hash table cnt to record the word count for each sender. Then, we traverse the hash table to find the sender with the highest word count. If there are multiple senders with the highest word count, we return the name that is lexicographically largest.

The time complexity is O(n + L), and the space complexity is O(n), where n is the number of messages and L is the total length of all messages.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Divide and Conquer

Time Complexity: O(n log n) - The algorithm splits the array into halves log n times, and for each half, it performs a linear merge.
Space Complexity: O(n) - Temporary arrays are used to store sorted sub-elements.

Approach 2: Dynamic Programming

Time Complexity: O(n) - Each Fibonacci term is computed only once.
Space Complexity: O(n) - Extra memory is used to store solutions to sub-problems.

Hash Table + Enumeration

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Divide and Conquer AggregationO(n·m)O(k)Useful when processing large message datasets in parallel or distributed systems
Hash Table Incremental Counting (Dynamic Programming)O(n·m)O(k)Best general solution; single pass with constant-time updates

Video Solution

Biweekly Contest 79 || A - 2283, B - 2284, C - 2285 Solution || Hindi ExplanationBinaryMagic547 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Sender With Largest Word Count easy or hard?
The problem is typically classified as Medium because it combines string processing, counting logic, and tie-breaking rules. The algorithm itself is straightforward, but handling word counting and lexicographic comparison correctly is where mistakes often happen.
Sender With Largest Word Count Python/Java solution
In Python, use a dictionary to map sender names to word totals and compute word counts using split() or space counting. In Java, use HashMap<String, Integer> and update counts with getOrDefault. Both implementations run in O(n·m) time and O(k) space.
How to solve Sender With Largest Word Count in O(n)?
Treat each message as one processing step and maintain a hash map of sender → total word count. Count the words in the current message, add them to the sender’s total, and update the current best sender if needed. Because each message is processed once, the algorithm is linear in the number of messages, with extra cost proportional to message length.
What is the best approach for Sender With Largest Word Count?
The most efficient approach uses a hash table to accumulate word counts per sender. Iterate through the messages once, count the words in each message, and update the sender's total. Track the maximum count and resolve ties using lexicographic comparison. This solution runs in O(n·m) time and O(k) space where k is the number of unique senders.
Is Sender With Largest Word Count asked at Google/Amazon/Meta?
Problems involving frequency counting with hash maps are common in interviews at companies like Amazon, Google, and Meta. Variants often involve aggregating metrics by key and applying tie-breaking rules. This problem tests string processing, hash table usage, and careful comparison logic.
What data structure is used in Sender With Largest Word Count?
A hash table (HashMap in Java, unordered_map in C++, dict in Python) stores the cumulative word count for each sender. This allows constant-time updates and efficient lookup when aggregating totals.
What is the time complexity of Sender With Largest Word Count?
The time complexity is O(n·m), where n is the number of messages and m is the average length of a message. Each message must be scanned to count its words. Hash table updates and comparisons occur in constant average time.

Ready to solve this problem?

Practice Sender With Largest Word Count with our built-in code editor and test cases.

Practice on FleetCode