Skip to main content
Back to problems
#2284
Medium Algorithms

Sender with largest word count

Array Hash Table String Counting
59.5% acceptance
Feb 25, 2026
466
41
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 that is lexicographically the largest.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
use std::collections::HashMap;


impl Solution {
  pub fn largest_word_count(messages: Vec<String>, senders: Vec<String>) -> String {
    let mut count: HashMap<String, i32> = HashMap::new();
    for (msg, sender) in messages.iter().zip(senders.iter()) {
      let words = msg.split_whitespace().count() as i32;
      *count.entry(sender.clone()).or_insert(0) += words;
    }
    let mut best = String::new();
    let mut best_count = 0;
    for (sender, cnt) in &count {
      if *cnt > best_count || (*cnt == best_count && sender > &best) {
        best = sender.clone();
        best_count = *cnt;
      }
    }
    best
  }
}