Problem

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 thelargest word count. If there is more than one sender with the largest word count, return the one with thelexicographically largest name.

Note:

  • Uppercase letters come before lowercase letters in lexicographical order.
  • "Alice" and "alice" are distinct.

Examples

Example 1

1
2
3
4
5
6
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

1
2
3
4
5
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 <= 10^4
  • 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.

Solution

Method 1 – Hash Map Counting

Intuition

Count the total number of words sent by each sender. Then, return the sender with the largest word count, breaking ties by lexicographically largest name.

Approach

  1. For each message, count the number of words (split by space) and add to the sender’s total in a hash map.
  2. Track the sender with the largest word count, breaking ties by lexicographically largest name.
  3. Return the answer.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    string largestWordCount(vector<string>& messages, vector<string>& senders) {
        unordered_map<string, int> cnt;
        int n = messages.size();
        for (int i = 0; i < n; ++i) {
            int words = 1;
            for (char c : messages[i]) if (c == ' ') ++words;
            cnt[senders[i]] += words;
        }
        string ans;
        int mx = 0;
        for (auto& [k, v] : cnt) {
            if (v > mx || (v == mx && k > ans)) {
                mx = v;
                ans = k;
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
func largestWordCount(messages []string, senders []string) string {
    cnt := map[string]int{}
    for i, msg := range messages {
        words := 1
        for _, c := range msg {
            if c == ' ' { words++ }
        }
        cnt[senders[i]] += words
    }
    ans := ""
    mx := 0
    for k, v := range cnt {
        if v > mx || (v == mx && k > ans) {
            mx = v
            ans = k
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;
class Solution {
    public String largestWordCount(String[] messages, String[] senders) {
        Map<String, Integer> cnt = new HashMap<>();
        for (int i = 0; i < messages.length; ++i) {
            int words = messages[i].split(" ").length;
            cnt.put(senders[i], cnt.getOrDefault(senders[i], 0) + words);
        }
        String ans = "";
        int mx = 0;
        for (String k : cnt.keySet()) {
            int v = cnt.get(k);
            if (v > mx || (v == mx && k.compareTo(ans) > 0)) {
                mx = v;
                ans = k;
            }
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    fun largestWordCount(messages: Array<String>, senders: Array<String>): String {
        val cnt = mutableMapOf<String, Int>()
        for (i in messages.indices) {
            val words = messages[i].split(" ").size
            cnt[senders[i]] = cnt.getOrDefault(senders[i], 0) + words
        }
        var ans = ""
        var mx = 0
        for ((k, v) in cnt) {
            if (v > mx || (v == mx && k > ans)) {
                mx = v
                ans = k
            }
        }
        return ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def largestWordCount(self, messages: list[str], senders: list[str]) -> str:
        cnt = {}
        for msg, snd in zip(messages, senders):
            words = msg.count(' ') + 1
            cnt[snd] = cnt.get(snd, 0) + words
        mx = 0
        ans = ''
        for k, v in cnt.items():
            if v > mx or (v == mx and k > ans):
                mx = v
                ans = k
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
use std::collections::HashMap;
impl Solution {
    pub fn largest_word_count(messages: Vec<String>, senders: Vec<String>) -> String {
        let mut cnt = HashMap::new();
        for (msg, snd) in messages.iter().zip(senders.iter()) {
            let words = msg.chars().filter(|&c| c == ' ').count() + 1;
            *cnt.entry(snd).or_insert(0) += words;
        }
        let mut ans = "".to_string();
        let mut mx = 0;
        for (k, &v) in &cnt {
            if v > mx || (v == mx && k > &ans) {
                mx = v;
                ans = k.clone();
            }
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    largestWordCount(messages: string[], senders: string[]): string {
        const cnt = new Map<string, number>();
        for (let i = 0; i < messages.length; ++i) {
            const words = messages[i].split(' ').length;
            cnt.set(senders[i], (cnt.get(senders[i]) ?? 0) + words);
        }
        let ans = '';
        let mx = 0;
        for (const [k, v] of cnt) {
            if (v > mx || (v === mx && k > ans)) {
                mx = v;
                ans = k;
            }
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n * m), where n = number of messages, m = average message length. Each message is split and counted.
  • 🧺 Space complexity: O(n), for the hash map of senders.