-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2284-sender-with-largest-word-count.py
More file actions
28 lines (23 loc) · 992 Bytes
/
2284-sender-with-largest-word-count.py
File metadata and controls
28 lines (23 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# time complexity: O(n)
# space complexity: O(n)
from collections import defaultdict
from typing import List
class Solution:
def largestWordCount(self, messages: List[str], senders: List[str]) -> str:
senderDict = defaultdict(int)
for i, sender in enumerate(senders):
wordCount = len(messages[i].split(" "))
senderDict[sender] += wordCount
maxWordCount = max(senderDict.values())
result = ""
for sender, wordCount in senderDict.items():
if wordCount == maxWordCount:
result = max(result, sender)
return result
messages = ["Hello userTwooo", "Hi userThree",
"Wonderful day Alice", "Nice day userThree"]
senders = ["Alice", "userTwo", "userThree", "Alice"]
print(Solution().largestWordCount(messages, senders))
messages = ["How is leetcode for everyone", "Leetcode is useful for practice"]
senders = ["Bob", "Charlie"]
print(Solution().largestWordCount(messages, senders))