From 167c2be2abfaab057d1c3abd2d8918fb822f0e2c Mon Sep 17 00:00:00 2001 From: AmruthaPariprolu Date: Thu, 25 Jul 2024 19:38:58 +0530 Subject: [PATCH 1/5] solution added --- .../2284-Sender-With-Largest-Word-Count.md | 346 ++++++++++++++++++ 1 file changed, 346 insertions(+) create mode 100644 dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md diff --git a/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md b/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md new file mode 100644 index 000000000..8b4f70d3b --- /dev/null +++ b/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md @@ -0,0 +1,346 @@ +--- +id: Sender-With-Largest-Word-Count +title: Sender With Largest Word Count +sidebar_label: 2284-Sender With Largest Word Count +tags: + - Arrays + - Brute Force + - Optimized approach + - LeetCode + - Python + - Java + - C++ + +description: "This is a solution to Sender With Largest Word Count problem on LeetCode." +sidebar_position: 85 +--- + +## Problem Statement +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++. + +### Problem Description + +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]. + +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. + +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. + +### Examples + +**Example 1:** +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. +userTwo sends a total of 2 words. +userThree sends a total of 3 words. +Since Alice has the largest word count, we return "Alice". +**Example 2:** +Input: messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"] +Output: "Charlie" +Explanation: Bob sends a total of 5 words. +Charlie sends a total of 5 words. +Since there is a tie for the largest word count, we return the sender with the lexicographically larger name, Charlie. + +### Constraints +- `n == messages.length == senders.length` +- `1 <= n <= 104` +- `1 <= messages[i].length <= 100` +- `1 <= senders[i].length <= 10` +- `messages[i] consists of uppercase and lowercase English letters and ' '.` +- `All the words in messages[i] are separated by a single space.` +- `messages[i] does not have leading or trailing spaces.` +- `senders[i] consists of uppercase and lowercase English letters only.` +## Solution of Given Problem + +### Intuition and Approach + +The problem can be solved using a brute force approach or an optimized Technique. + + + + +### Approach 1:Brute Force (Naive) + + +Count Words for Each Sender: + +Iterate through the messages array. +For each message, count the number of words and add this count to the corresponding sender's total in a dictionary. +Determine the Sender with the Largest Word Count: + +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. +#### Codes in Different Languages + + + + + +```cpp +#include +#include +#include +#include +#include + +std::string largestWordCount(std::vector& messages, std::vector& senders) { + std::unordered_map wordCount; + + for (int i = 0; i < messages.size(); i++) { + std::istringstream iss(messages[i]); + int count = 0; + std::string word; + while (iss >> word) count++; + wordCount[senders[i]] += count; + } + + std::string result; + int maxCount = 0; + + for (const auto& entry : wordCount) { + if (entry.second > maxCount || (entry.second == maxCount && entry.first > result)) { + maxCount = entry.second; + result = entry.first; + } + } + + return result; +} + +``` + + + + +```java +import java.util.*; + +public class Solution { + public String largestWordCount(String[] messages, String[] senders) { + Map wordCount = new HashMap<>(); + + for (int i = 0; i < messages.length; i++) { + int count = messages[i].split(" ").length; + wordCount.put(senders[i], wordCount.getOrDefault(senders[i], 0) + count); + } + + String result = ""; + int maxCount = 0; + + for (Map.Entry entry : wordCount.entrySet()) { + if (entry.getValue() > maxCount || (entry.getValue() == maxCount && entry.getKey().compareTo(result) > 0)) { + maxCount = entry.getValue(); + result = entry.getKey(); + } + } + + return result; + } +} + + + +``` + + + + + + +```python +from collections import defaultdict + +def largest_word_count(messages, senders): + word_count = defaultdict(int) + + for i in range(len(messages)): + count = len(messages[i].split()) + word_count[senders[i]] += count + + max_count = 0 + result = "" + + for sender, count in word_count.items(): + if count > max_count or (count == max_count and sender > result): + max_count = count + result = sender + + return result + +``` + + + + + +### Complexity Analysis + +- Time Complexity: $O(n+k)$ +- where n is the number of messages and k is the number of unique senders. +- Space Complexity: $O(k)$ +- for storing word counts of senders. + + + + +### Approach 2: Optimized approach + +Optimized Approach: Word Count Calculation: +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. +Tracking Maximum Word Count and Sender: +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. + +#### Code in Different Languages + + + + + +```cpp +#include +#include +#include +#include +#include + +std::string largestWordCount(std::vector& messages, std::vector& senders) { + std::unordered_map wordCount; + std::string result; + int maxCount = 0; + + for (int i = 0; i < messages.size(); i++) { + int count = std::count(messages[i].begin(), messages[i].end(), ' ') + 1; + wordCount[senders[i]] += count; + + if (wordCount[senders[i]] > maxCount || (wordCount[senders[i]] == maxCount && senders[i] > result)) { + maxCount = wordCount[senders[i]]; + result = senders[i]; + } + } + + return result; +} + + + + +``` + + + + +```java +import java.util.*; + +public class Solution { + public String largestWordCount(String[] messages, String[] senders) { + Map wordCount = new HashMap<>(); + String result = ""; + int maxCount = 0; + + for (int i = 0; i < messages.length; i++) { + int count = messages[i].split(" ").length; + wordCount.put(senders[i], wordCount.getOrDefault(senders[i], 0) + count); + + if (wordCount.get(senders[i]) > maxCount || + (wordCount.get(senders[i]) == maxCount && senders[i].compareTo(result) > 0)) { + maxCount = wordCount.get(senders[i]); + result = senders[i]; + } + } + + return result; + } +} + +``` + + + + + + +```python +from collections import defaultdict + +def largest_word_count(messages, senders): + word_count = defaultdict(int) + max_count = 0 + result = "" + + for message, sender in zip(messages, senders): + count = len(message.split()) + word_count[sender] += count + + if word_count[sender] > max_count or (word_count[sender] == max_count and sender > result): + max_count = word_count[sender] + result = sender + + return result + + +``` + + + + +#### Complexity Analysis + +- Time Complexity: $O(n+k)$ + +- Space Complexity: $O(k)$ + +- This approach is efficient and straightforward. + + + + + +## Video Explanation of Given Problem + + + + + + + + + + + + + + + + + + + + +--- + +

Authors:

+ +
+{['AmruthaPariprolu'].map(username => ( + +))} +
From 348bf8ba8bf847006c569f5fce679a8edf152ff9 Mon Sep 17 00:00:00 2001 From: Amrutha <159519629+AmruthaPariprolu@users.noreply.github.com> Date: Thu, 25 Jul 2024 19:40:32 +0530 Subject: [PATCH 2/5] Update 2284-Sender-With-Largest-Word-Count.md --- .../2200-2299/2284-Sender-With-Largest-Word-Count.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md b/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md index 8b4f70d3b..2ce622d60 100644 --- a/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md +++ b/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md @@ -3,7 +3,7 @@ id: Sender-With-Largest-Word-Count title: Sender With Largest Word Count sidebar_label: 2284-Sender With Largest Word Count tags: - - Arrays + - Strings - Brute Force - Optimized approach - LeetCode From fdadb3f0583c13cd5c576e787c4b5b6bcaae144e Mon Sep 17 00:00:00 2001 From: Amrutha <159519629+AmruthaPariprolu@users.noreply.github.com> Date: Thu, 25 Jul 2024 19:43:50 +0530 Subject: [PATCH 3/5] Update 2284-Sender-With-Largest-Word-Count.md --- .../2200-2299/2284-Sender-With-Largest-Word-Count.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md b/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md index 2ce622d60..e3febec17 100644 --- a/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md +++ b/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md @@ -303,7 +303,7 @@ def largest_word_count(messages, senders): Date: Thu, 25 Jul 2024 19:48:56 +0530 Subject: [PATCH 4/5] Update 2284-Sender-With-Largest-Word-Count.md --- .../2200-2299/2284-Sender-With-Largest-Word-Count.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md b/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md index e3febec17..a5a9af923 100644 --- a/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md +++ b/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md @@ -292,8 +292,7 @@ def largest_word_count(messages, senders): - This approach is efficient and straightforward. - - + ## Video Explanation of Given Problem From 5d607bf4653dd90621e0385a876f6f8cccb8538b Mon Sep 17 00:00:00 2001 From: Ajay Dhangar <99037494+Ajay-Dhangar@users.noreply.github.com> Date: Fri, 26 Jul 2024 13:10:04 +0530 Subject: [PATCH 5/5] Update 2284-Sender-With-Largest-Word-Count.md --- .../2284-Sender-With-Largest-Word-Count.md | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md b/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md index a5a9af923..14a36961a 100644 --- a/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md +++ b/dsa-solutions/lc-solutions/2200-2299/2284-Sender-With-Largest-Word-Count.md @@ -16,7 +16,7 @@ sidebar_position: 85 --- ## Problem Statement -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++. +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++. ### Problem Description @@ -29,7 +29,7 @@ Return the sender with the largest word count. If there is more than one sender ### Examples **Example 1:** -Input: messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders = ["Alice","userTwo","userThree","Alice"] +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. userTwo sends a total of 2 words. @@ -57,11 +57,7 @@ Since there is a tie for the largest word count, we return the sender with the l The problem can be solved using a brute force approach or an optimized Technique. - - - -### Approach 1:Brute Force (Naive) - +## Approach 1:Brute Force (Naive) Count Words for Each Sender: @@ -180,10 +176,7 @@ def largest_word_count(messages, senders): - Space Complexity: $O(k)$ - for storing word counts of senders. - - - -### Approach 2: Optimized approach +## Approach 2: Optimized approach Optimized Approach: Word Count Calculation: 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. @@ -292,9 +285,6 @@ def largest_word_count(messages, senders): - This approach is efficient and straightforward. - - - ## Video Explanation of Given Problem @@ -332,8 +322,6 @@ def largest_word_count(messages, senders): - - ---

Authors: