Skip to content

Commit 167c2be

Browse files
solution added
1 parent 4a2db7a commit 167c2be

File tree

1 file changed

+346
-0
lines changed

1 file changed

+346
-0
lines changed
Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
---
2+
id: Sender-With-Largest-Word-Count
3+
title: Sender With Largest Word Count
4+
sidebar_label: 2284-Sender With Largest Word Count
5+
tags:
6+
- Arrays
7+
- Brute Force
8+
- Optimized approach
9+
- LeetCode
10+
- Python
11+
- Java
12+
- C++
13+
14+
description: "This is a solution to Sender With Largest Word Count problem on LeetCode."
15+
sidebar_position: 85
16+
---
17+
18+
## Problem Statement
19+
In this tutorial, we will solve the Sender With Largest Word Count problem . We will provide the implementation of the solution in Python, Java, and C++.
20+
21+
### Problem Description
22+
23+
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].
24+
25+
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.
26+
27+
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.
28+
29+
### Examples
30+
31+
**Example 1:**
32+
Input: messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders = ["Alice","userTwo","userThree","Alice"]
33+
Output: "Alice"
34+
Explanation: Alice sends a total of 2 + 3 = 5 words.
35+
userTwo sends a total of 2 words.
36+
userThree sends a total of 3 words.
37+
Since Alice has the largest word count, we return "Alice".
38+
**Example 2:**
39+
Input: messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"]
40+
Output: "Charlie"
41+
Explanation: Bob sends a total of 5 words.
42+
Charlie sends a total of 5 words.
43+
Since there is a tie for the largest word count, we return the sender with the lexicographically larger name, Charlie.
44+
45+
### Constraints
46+
- `n == messages.length == senders.length`
47+
- `1 <= n <= 104`
48+
- `1 <= messages[i].length <= 100`
49+
- `1 <= senders[i].length <= 10`
50+
- `messages[i] consists of uppercase and lowercase English letters and ' '.`
51+
- `All the words in messages[i] are separated by a single space.`
52+
- `messages[i] does not have leading or trailing spaces.`
53+
- `senders[i] consists of uppercase and lowercase English letters only.`
54+
## Solution of Given Problem
55+
56+
### Intuition and Approach
57+
58+
The problem can be solved using a brute force approach or an optimized Technique.
59+
60+
<Tabs>
61+
<tabItem value="Brute Force" label="Brute Force">
62+
63+
### Approach 1:Brute Force (Naive)
64+
65+
66+
Count Words for Each Sender:
67+
68+
Iterate through the messages array.
69+
For each message, count the number of words and add this count to the corresponding sender's total in a dictionary.
70+
Determine the Sender with the Largest Word Count:
71+
72+
Traverse the dictionary to find the sender with the maximum word count. In case of a tie, choose the sender with the lexicographically larger name.
73+
#### Codes in Different Languages
74+
75+
<Tabs>
76+
<TabItem value="C++" label="C++" default>
77+
<SolutionAuthor name="@AmruthaPariprolu"/>
78+
79+
```cpp
80+
#include <iostream>
81+
#include <vector>
82+
#include <unordered_map>
83+
#include <sstream>
84+
#include <algorithm>
85+
86+
std::string largestWordCount(std::vector<std::string>& messages, std::vector<std::string>& senders) {
87+
std::unordered_map<std::string, int> wordCount;
88+
89+
for (int i = 0; i < messages.size(); i++) {
90+
std::istringstream iss(messages[i]);
91+
int count = 0;
92+
std::string word;
93+
while (iss >> word) count++;
94+
wordCount[senders[i]] += count;
95+
}
96+
97+
std::string result;
98+
int maxCount = 0;
99+
100+
for (const auto& entry : wordCount) {
101+
if (entry.second > maxCount || (entry.second == maxCount && entry.first > result)) {
102+
maxCount = entry.second;
103+
result = entry.first;
104+
}
105+
}
106+
107+
return result;
108+
}
109+
110+
```
111+
</TabItem>
112+
<TabItem value="Java" label="Java">
113+
<SolutionAuthor name="@AmruthaPariprolu"/>
114+
115+
```java
116+
import java.util.*;
117+
118+
public class Solution {
119+
public String largestWordCount(String[] messages, String[] senders) {
120+
Map<String, Integer> wordCount = new HashMap<>();
121+
122+
for (int i = 0; i < messages.length; i++) {
123+
int count = messages[i].split(" ").length;
124+
wordCount.put(senders[i], wordCount.getOrDefault(senders[i], 0) + count);
125+
}
126+
127+
String result = "";
128+
int maxCount = 0;
129+
130+
for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
131+
if (entry.getValue() > maxCount || (entry.getValue() == maxCount && entry.getKey().compareTo(result) > 0)) {
132+
maxCount = entry.getValue();
133+
result = entry.getKey();
134+
}
135+
}
136+
137+
return result;
138+
}
139+
}
140+
141+
142+
143+
```
144+
145+
146+
</TabItem>
147+
<TabItem value="Python" label="Python">
148+
<SolutionAuthor name="@AmruthaPariprolu"/>
149+
150+
```python
151+
from collections import defaultdict
152+
153+
def largest_word_count(messages, senders):
154+
word_count = defaultdict(int)
155+
156+
for i in range(len(messages)):
157+
count = len(messages[i].split())
158+
word_count[senders[i]] += count
159+
160+
max_count = 0
161+
result = ""
162+
163+
for sender, count in word_count.items():
164+
if count > max_count or (count == max_count and sender > result):
165+
max_count = count
166+
result = sender
167+
168+
return result
169+
170+
```
171+
172+
</TabItem>
173+
</Tabs>
174+
175+
176+
### Complexity Analysis
177+
178+
- Time Complexity: $O(n+k)$
179+
- where n is the number of messages and k is the number of unique senders.
180+
- Space Complexity: $O(k)$
181+
- for storing word counts of senders.
182+
183+
</tabItem>
184+
<tabItem value="Optimized approach" label="Optimized approach">
185+
186+
### Approach 2: Optimized approach
187+
188+
Optimized Approach: Word Count Calculation:
189+
For each message, split the message string and count the words. This step is O(1) in complexity since the maximum length of a message is fixed.
190+
Tracking Maximum Word Count and Sender:
191+
Keep track of the maximum word count and the sender with that count directly while iterating through the dictionary. This avoids the need for a separate loop to find the maximum.
192+
193+
#### Code in Different Languages
194+
195+
<Tabs>
196+
<TabItem value="C++" label="C++" default>
197+
<SolutionAuthor name="@AmruthaPariprolu"/>
198+
199+
```cpp
200+
#include <iostream>
201+
#include <vector>
202+
#include <unordered_map>
203+
#include <sstream>
204+
#include <algorithm>
205+
206+
std::string largestWordCount(std::vector<std::string>& messages, std::vector<std::string>& senders) {
207+
std::unordered_map<std::string, int> wordCount;
208+
std::string result;
209+
int maxCount = 0;
210+
211+
for (int i = 0; i < messages.size(); i++) {
212+
int count = std::count(messages[i].begin(), messages[i].end(), ' ') + 1;
213+
wordCount[senders[i]] += count;
214+
215+
if (wordCount[senders[i]] > maxCount || (wordCount[senders[i]] == maxCount && senders[i] > result)) {
216+
maxCount = wordCount[senders[i]];
217+
result = senders[i];
218+
}
219+
}
220+
221+
return result;
222+
}
223+
224+
225+
226+
227+
```
228+
</TabItem>
229+
<TabItem value="Java" label="Java">
230+
<SolutionAuthor name="@AmruthaPariprolu"/>
231+
232+
```java
233+
import java.util.*;
234+
235+
public class Solution {
236+
public String largestWordCount(String[] messages, String[] senders) {
237+
Map<String, Integer> wordCount = new HashMap<>();
238+
String result = "";
239+
int maxCount = 0;
240+
241+
for (int i = 0; i < messages.length; i++) {
242+
int count = messages[i].split(" ").length;
243+
wordCount.put(senders[i], wordCount.getOrDefault(senders[i], 0) + count);
244+
245+
if (wordCount.get(senders[i]) > maxCount ||
246+
(wordCount.get(senders[i]) == maxCount && senders[i].compareTo(result) > 0)) {
247+
maxCount = wordCount.get(senders[i]);
248+
result = senders[i];
249+
}
250+
}
251+
252+
return result;
253+
}
254+
}
255+
256+
```
257+
258+
259+
</TabItem>
260+
<TabItem value="Python" label="Python">
261+
<SolutionAuthor name="@AmruthaPariprolu"/>
262+
263+
```python
264+
from collections import defaultdict
265+
266+
def largest_word_count(messages, senders):
267+
word_count = defaultdict(int)
268+
max_count = 0
269+
result = ""
270+
271+
for message, sender in zip(messages, senders):
272+
count = len(message.split())
273+
word_count[sender] += count
274+
275+
if word_count[sender] > max_count or (word_count[sender] == max_count and sender > result):
276+
max_count = word_count[sender]
277+
result = sender
278+
279+
return result
280+
281+
282+
```
283+
284+
</TabItem>
285+
</Tabs>
286+
287+
#### Complexity Analysis
288+
289+
- Time Complexity: $O(n+k)$
290+
291+
- Space Complexity: $O(k)$
292+
293+
- This approach is efficient and straightforward.
294+
295+
</tabItem>
296+
</Tabs>
297+
298+
299+
## Video Explanation of Given Problem
300+
301+
<Tabs>
302+
<TabItem value="en" label="English">
303+
<Tabs>
304+
<TabItem value="c++" label="C++">
305+
<LiteYouTubeEmbed
306+
id="https://youtu.be/aH2bWWV_KVk?si=TKIN3grMJsQy8Ujw"
307+
params="autoplay=1&autohide=1&showinfo=0&rel=0"
308+
title="Problem Explanation | Solution | Approach"
309+
poster="maxresdefault"
310+
webp
311+
/>
312+
</TabItem>
313+
<TabItem value="java" label="Java">
314+
<LiteYouTubeEmbed
315+
id="https://youtu.be/GYZNCUVQOJo?si=skYtdNag51nGkKjs"
316+
params="autoplay=1&autohide=1&showinfo=0&rel=0"
317+
title="Problem Explanation | Solution | Approach"
318+
poster="maxresdefault"
319+
webp
320+
/>
321+
</TabItem>
322+
<TabItem value="python" label="Python">
323+
<LiteYouTubeEmbed
324+
id="https://youtu.be/h5-nuBDpHjI?si=RTRLPTqsLUcyB-yL"
325+
params="autoplay=1&autohide=1&showinfo=0&rel=0"
326+
title="Problem Explanation | Solution | Approach"
327+
poster="maxresdefault"
328+
webp
329+
/>
330+
</TabItem>
331+
</Tabs>
332+
</TabItem>
333+
</Tabs>
334+
335+
336+
337+
338+
---
339+
340+
<h2>Authors:</h2>
341+
342+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
343+
{['AmruthaPariprolu'].map(username => (
344+
<Author key={username} username={username} />
345+
))}
346+
</div>

0 commit comments

Comments
 (0)