|
| 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 | + - Strings |
| 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 | +## Approach 1:Brute Force (Naive) |
| 61 | + |
| 62 | +Count Words for Each Sender: |
| 63 | + |
| 64 | +Iterate through the messages array. |
| 65 | +For each message, count the number of words and add this count to the corresponding sender's total in a dictionary. |
| 66 | +Determine the Sender with the Largest Word Count: |
| 67 | + |
| 68 | +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. |
| 69 | +#### Codes in Different Languages |
| 70 | + |
| 71 | +<Tabs> |
| 72 | +<TabItem value="C++" label="C++" default> |
| 73 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 74 | + |
| 75 | +```cpp |
| 76 | +#include <iostream> |
| 77 | +#include <vector> |
| 78 | +#include <unordered_map> |
| 79 | +#include <sstream> |
| 80 | +#include <algorithm> |
| 81 | + |
| 82 | +std::string largestWordCount(std::vector<std::string>& messages, std::vector<std::string>& senders) { |
| 83 | + std::unordered_map<std::string, int> wordCount; |
| 84 | + |
| 85 | + for (int i = 0; i < messages.size(); i++) { |
| 86 | + std::istringstream iss(messages[i]); |
| 87 | + int count = 0; |
| 88 | + std::string word; |
| 89 | + while (iss >> word) count++; |
| 90 | + wordCount[senders[i]] += count; |
| 91 | + } |
| 92 | + |
| 93 | + std::string result; |
| 94 | + int maxCount = 0; |
| 95 | + |
| 96 | + for (const auto& entry : wordCount) { |
| 97 | + if (entry.second > maxCount || (entry.second == maxCount && entry.first > result)) { |
| 98 | + maxCount = entry.second; |
| 99 | + result = entry.first; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return result; |
| 104 | +} |
| 105 | + |
| 106 | +``` |
| 107 | +</TabItem> |
| 108 | +<TabItem value="Java" label="Java"> |
| 109 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 110 | +
|
| 111 | +```java |
| 112 | +import java.util.*; |
| 113 | +
|
| 114 | +public class Solution { |
| 115 | + public String largestWordCount(String[] messages, String[] senders) { |
| 116 | + Map<String, Integer> wordCount = new HashMap<>(); |
| 117 | +
|
| 118 | + for (int i = 0; i < messages.length; i++) { |
| 119 | + int count = messages[i].split(" ").length; |
| 120 | + wordCount.put(senders[i], wordCount.getOrDefault(senders[i], 0) + count); |
| 121 | + } |
| 122 | +
|
| 123 | + String result = ""; |
| 124 | + int maxCount = 0; |
| 125 | +
|
| 126 | + for (Map.Entry<String, Integer> entry : wordCount.entrySet()) { |
| 127 | + if (entry.getValue() > maxCount || (entry.getValue() == maxCount && entry.getKey().compareTo(result) > 0)) { |
| 128 | + maxCount = entry.getValue(); |
| 129 | + result = entry.getKey(); |
| 130 | + } |
| 131 | + } |
| 132 | +
|
| 133 | + return result; |
| 134 | + } |
| 135 | +} |
| 136 | +
|
| 137 | +
|
| 138 | +
|
| 139 | +``` |
| 140 | + |
| 141 | + |
| 142 | +</TabItem> |
| 143 | +<TabItem value="Python" label="Python"> |
| 144 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 145 | + |
| 146 | +```python |
| 147 | +from collections import defaultdict |
| 148 | + |
| 149 | +def largest_word_count(messages, senders): |
| 150 | + word_count = defaultdict(int) |
| 151 | + |
| 152 | + for i in range(len(messages)): |
| 153 | + count = len(messages[i].split()) |
| 154 | + word_count[senders[i]] += count |
| 155 | + |
| 156 | + max_count = 0 |
| 157 | + result = "" |
| 158 | + |
| 159 | + for sender, count in word_count.items(): |
| 160 | + if count > max_count or (count == max_count and sender > result): |
| 161 | + max_count = count |
| 162 | + result = sender |
| 163 | + |
| 164 | + return result |
| 165 | + |
| 166 | +``` |
| 167 | + |
| 168 | +</TabItem> |
| 169 | +</Tabs> |
| 170 | + |
| 171 | + |
| 172 | +### Complexity Analysis |
| 173 | + |
| 174 | +- Time Complexity: $O(n+k)$ |
| 175 | +- where n is the number of messages and k is the number of unique senders. |
| 176 | +- Space Complexity: $O(k)$ |
| 177 | +- for storing word counts of senders. |
| 178 | + |
| 179 | +## Approach 2: Optimized approach |
| 180 | + |
| 181 | +Optimized Approach: Word Count Calculation: |
| 182 | +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. |
| 183 | +Tracking Maximum Word Count and Sender: |
| 184 | +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. |
| 185 | + |
| 186 | +#### Code in Different Languages |
| 187 | + |
| 188 | +<Tabs> |
| 189 | +<TabItem value="C++" label="C++" default> |
| 190 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 191 | + |
| 192 | +```cpp |
| 193 | +#include <iostream> |
| 194 | +#include <vector> |
| 195 | +#include <unordered_map> |
| 196 | +#include <sstream> |
| 197 | +#include <algorithm> |
| 198 | + |
| 199 | +std::string largestWordCount(std::vector<std::string>& messages, std::vector<std::string>& senders) { |
| 200 | + std::unordered_map<std::string, int> wordCount; |
| 201 | + std::string result; |
| 202 | + int maxCount = 0; |
| 203 | + |
| 204 | + for (int i = 0; i < messages.size(); i++) { |
| 205 | + int count = std::count(messages[i].begin(), messages[i].end(), ' ') + 1; |
| 206 | + wordCount[senders[i]] += count; |
| 207 | + |
| 208 | + if (wordCount[senders[i]] > maxCount || (wordCount[senders[i]] == maxCount && senders[i] > result)) { |
| 209 | + maxCount = wordCount[senders[i]]; |
| 210 | + result = senders[i]; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + return result; |
| 215 | +} |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | +``` |
| 221 | +</TabItem> |
| 222 | +<TabItem value="Java" label="Java"> |
| 223 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 224 | +
|
| 225 | +```java |
| 226 | +import java.util.*; |
| 227 | +
|
| 228 | +public class Solution { |
| 229 | + public String largestWordCount(String[] messages, String[] senders) { |
| 230 | + Map<String, Integer> wordCount = new HashMap<>(); |
| 231 | + String result = ""; |
| 232 | + int maxCount = 0; |
| 233 | +
|
| 234 | + for (int i = 0; i < messages.length; i++) { |
| 235 | + int count = messages[i].split(" ").length; |
| 236 | + wordCount.put(senders[i], wordCount.getOrDefault(senders[i], 0) + count); |
| 237 | +
|
| 238 | + if (wordCount.get(senders[i]) > maxCount || |
| 239 | + (wordCount.get(senders[i]) == maxCount && senders[i].compareTo(result) > 0)) { |
| 240 | + maxCount = wordCount.get(senders[i]); |
| 241 | + result = senders[i]; |
| 242 | + } |
| 243 | + } |
| 244 | +
|
| 245 | + return result; |
| 246 | + } |
| 247 | +} |
| 248 | +
|
| 249 | +``` |
| 250 | + |
| 251 | + |
| 252 | +</TabItem> |
| 253 | +<TabItem value="Python" label="Python"> |
| 254 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 255 | + |
| 256 | +```python |
| 257 | +from collections import defaultdict |
| 258 | + |
| 259 | +def largest_word_count(messages, senders): |
| 260 | + word_count = defaultdict(int) |
| 261 | + max_count = 0 |
| 262 | + result = "" |
| 263 | + |
| 264 | + for message, sender in zip(messages, senders): |
| 265 | + count = len(message.split()) |
| 266 | + word_count[sender] += count |
| 267 | + |
| 268 | + if word_count[sender] > max_count or (word_count[sender] == max_count and sender > result): |
| 269 | + max_count = word_count[sender] |
| 270 | + result = sender |
| 271 | + |
| 272 | + return result |
| 273 | + |
| 274 | + |
| 275 | +``` |
| 276 | + |
| 277 | +</TabItem> |
| 278 | +</Tabs> |
| 279 | + |
| 280 | +#### Complexity Analysis |
| 281 | + |
| 282 | +- Time Complexity: $O(n+k)$ |
| 283 | + |
| 284 | +- Space Complexity: $O(k)$ |
| 285 | + |
| 286 | +- This approach is efficient and straightforward. |
| 287 | + |
| 288 | +## Video Explanation of Given Problem |
| 289 | + |
| 290 | +<Tabs> |
| 291 | + <TabItem value="en" label="English"> |
| 292 | + <Tabs> |
| 293 | + <TabItem value="c++" label="C++"> |
| 294 | + <LiteYouTubeEmbed |
| 295 | + id="aH2bWWV_KVk?si=TKIN3grMJsQy8Ujw" |
| 296 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 297 | + title="Problem Explanation | Solution | Approach" |
| 298 | + poster="maxresdefault" |
| 299 | + webp |
| 300 | + /> |
| 301 | + </TabItem> |
| 302 | + <TabItem value="java" label="Java"> |
| 303 | + <LiteYouTubeEmbed |
| 304 | + id="GYZNCUVQOJo?si=skYtdNag51nGkKjs" |
| 305 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 306 | + title="Problem Explanation | Solution | Approach" |
| 307 | + poster="maxresdefault" |
| 308 | + webp |
| 309 | + /> |
| 310 | + </TabItem> |
| 311 | + <TabItem value="python" label="Python"> |
| 312 | + <LiteYouTubeEmbed |
| 313 | + id="h5-nuBDpHjI?si=RTRLPTqsLUcyB-yL" |
| 314 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 315 | + title="Problem Explanation | Solution | Approach" |
| 316 | + poster="maxresdefault" |
| 317 | + webp |
| 318 | + /> |
| 319 | + </TabItem> |
| 320 | + </Tabs> |
| 321 | + </TabItem> |
| 322 | +</Tabs> |
| 323 | + |
| 324 | + |
| 325 | +--- |
| 326 | + |
| 327 | +<h2>Authors:</h2> |
| 328 | + |
| 329 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 330 | +{['AmruthaPariprolu'].map(username => ( |
| 331 | + <Author key={username} username={username} /> |
| 332 | +))} |
| 333 | +</div> |
0 commit comments