LeetCode 题解工作台
最多单词数的发件人
给你一个聊天记录,共包含 n 条信息。给你两个字符串数组 messages 和 senders ,其中 messages[i] 是 senders[i] 发出的一条 信息 。 一条 信息 是若干用单个空格连接的 单词 ,信息开头和结尾不会有多余空格。发件人的 单词计数 是这个发件人总共发出的 单词数…
4
题型
5
代码语言
3
相关题
当前训练重点
中等 · 数组·哈希·扫描
答案摘要
我们可以用一个哈希表 记录每个发件人的单词数,然后遍历哈希表找到单词数最多的发件人,如果有多个发件人发出最多单词数,我们返回字典序最大的名字。 时间复杂度 $O(n + L)$,空间复杂度 ,其中 是消息的数量,而 是所有消息的总长度。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
给你一个聊天记录,共包含 n 条信息。给你两个字符串数组 messages 和 senders ,其中 messages[i] 是 senders[i] 发出的一条 信息 。
一条 信息 是若干用单个空格连接的 单词 ,信息开头和结尾不会有多余空格。发件人的 单词计数 是这个发件人总共发出的 单词数 。注意,一个发件人可能会发出多于一条信息。
请你返回发出单词数 最多 的发件人名字。如果有多个发件人发出最多单词数,请你返回 字典序 最大的名字。
注意:
- 字典序里,大写字母小于小写字母。
"Alice"和"alice"是不同的名字。
示例 1:
输入:messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders = ["Alice","userTwo","userThree","Alice"] 输出:"Alice" 解释:Alice 总共发出了 2 + 3 = 5 个单词。 userTwo 发出了 2 个单词。 userThree 发出了 3 个单词。 由于 Alice 发出单词数最多,所以我们返回 "Alice" 。
示例 2:
输入:messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"] 输出:"Charlie" 解释:Bob 总共发出了 5 个单词。 Charlie 总共发出了 5 个单词。 由于最多单词数打平,返回字典序最大的名字,也就是 Charlie 。
提示:
n == messages.length == senders.length1 <= n <= 1041 <= messages[i].length <= 1001 <= senders[i].length <= 10messages[i]包含大写字母、小写字母和' '。messages[i]中所有单词都由 单个空格 隔开。messages[i]不包含前导和后缀空格。senders[i]只包含大写英文字母和小写英文字母。
解题思路
方法一:哈希表 + 枚举
我们可以用一个哈希表 记录每个发件人的单词数,然后遍历哈希表找到单词数最多的发件人,如果有多个发件人发出最多单词数,我们返回字典序最大的名字。
时间复杂度 ,空间复杂度 ,其中 是消息的数量,而 是所有消息的总长度。
class Solution:
def largestWordCount(self, messages: List[str], senders: List[str]) -> str:
cnt = Counter()
for message, sender in zip(messages, senders):
cnt[sender] += message.count(" ") + 1
ans = senders[0]
for k, v in cnt.items():
if cnt[ans] < v or (cnt[ans] == v and ans < k):
ans = k
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n * m) where n is the number of messages and m is the average number of words per message due to splitting. Space complexity is O(k) for storing counts per sender, with k being the number of unique senders. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Focus on efficient word counting with split operations and hash table aggregation.
- question_mark
Tie-breaking logic using lexicographical comparison is a critical detail.
- question_mark
Expect attention to cumulative counts across multiple messages per sender.
常见陷阱
外企场景- error
Failing to correctly count words by misinterpreting spaces or not handling multiple messages per sender.
- error
Ignoring tie-breakers and returning the first maximum instead of lexicographically largest.
- error
Updating counts incorrectly in the hash table, overwriting instead of accumulating.
进阶变体
外企场景- arrow_right_alt
Return the sender with the smallest word count instead of the largest.
- arrow_right_alt
Identify multiple senders with the top word count and return them in a sorted array.
- arrow_right_alt
Handle messages containing punctuation or multiple consecutive spaces requiring custom splitting logic.