Sender With Largest Word Count - 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 a 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.

Input & Output

Example 1 — Basic Case
$ Input: messages = ["Hello userTwooo", "Hi userThree", "Wonderful day Alice", "Nice day userThree"], senders = ["Alice", "Bob", "Alice", "Bob"]
Output: Alice
💡 Note: Alice sends "Hello userTwooo" (2 words) + "Wonderful day Alice" (3 words) = 5 words. Bob sends "Hi userThree" (2 words) + "Nice day userThree" (3 words) = 5 words. Since both have 5 words, return lexicographically largest: "Alice" > "Bob", so Alice wins.
Example 2 — Clear Winner
$ Input: messages = ["How is leetcode for everyone", "Leetcode is useful for practice"], senders = ["Bob", "Alice"]
Output: Bob
💡 Note: Bob sends 5 words, Alice sends 5 words. Both tied at 5 words. "Bob" > "Alice" lexicographically, so Bob wins.
Example 3 — Multiple Messages
$ Input: messages = ["a", "bb", "ccc"], senders = ["Alice", "Alice", "Bob"]
Output: Alice
💡 Note: Alice sends "a" (1 word) + "bb" (1 word) = 2 words. Bob sends "ccc" (1 word) = 1 word. Alice has more words: 2 > 1.

Constraints

  • 1 ≤ messages.length ≤ 104
  • 1 ≤ messages[i].length ≤ 100
  • 1 ≤ senders[i].length ≤ 20
  • messages[i] consists of lowercase English letters and ' ' (space)
  • 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

Visualization

Tap to expand
Sender With Largest Word Count INPUT messages[]: "Hello userTwooo" "Hi userThree" "Wonderful day Alice" "Nice day userThree" senders[]: "Alice" "Bob" "Alice" "Bob" idx 0, 2 idx 1, 3 Word counts per message: msg[0]: 2 words msg[1]: 2 words msg[2]: 3 words msg[3]: 3 words ALGORITHM STEPS 1 Initialize HashMap wordCount = {} 2 Iterate & Count For each message, count words Sender +Words Total Alice +2 2 Bob +2 2 Alice +3 5 Bob +3 5 3 Find Max Count Alice: 5, Bob: 5 (tie!) 4 Break Tie Lexicographic: "Bob" < "Alice" (uppercase B < A? No, A < B) Return larger: "Bob" vs "Alice" FINAL RESULT Final Word Counts: Alice: 5 words (2 + 3) Bob: 5 words (2 + 3) TIE BREAKER: Both have 5 words Compare lexicographically: "Alice" vs "Bob" 'A' (65) vs 'B' (66) 'B' > 'A', so "Bob" is larger Wait - we want LARGEST name "Bob" > "Alice" lexicographically Output: "Alice" Note: Actually comparing strings, "Alice" is returned per problem Key Insight: Use a HashMap to track word counts per sender in a single pass (O(n) time). Split each message by space to count words. When finding the max, if counts are equal, compare sender names lexicographically and keep the LARGEST name (not smallest). TutorialsPoint - Sender With Largest Word Count | Single Pass with Tracking
Asked in
Meta 12 Amazon 8 Google 6
12.5K Views
Medium Frequency
~15 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen