Sender With Largest Word Count — LeetCode 2284 Python Solution
MediumArrayHash TableStringCounting
- Problem
- #2284
- Pattern
- Hash Map
- Reading time
- 2 min
- Source
- leetcode.com
The 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].
Example
- 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.
Python solution
Python
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 ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n + L) |
| Space | O(n), where n is the number of messages and L is the total length of all messages auxiliary |
Pattern: Hash Map
Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2284. Sender With Largest Word Count is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table and Counting.
The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2284. Sender With Largest Word Count?
- LeetCode 2284. Sender With Largest Word Count is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2284. Sender With Largest Word Count?
- The Python solution on this page runs in O(n + L).
- What is the space complexity of LeetCode 2284. Sender With Largest Word Count?
- The Python solution on this page uses O(n), where n is the number of messages and L is the total length of all messages auxiliary space.
- What topics does LeetCode 2284. Sender With Largest Word Count cover?
- LeetCode 2284. Sender With Largest Word Count is tagged Array, Hash Table, String and Counting on LeetCode.