Similar Problems

Similar Problems not available

Sender With Largest Word Count - Leetcode Solution

Companies:

LeetCode:  Sender With Largest Word Count Leetcode Solution

Difficulty: Medium

Topics: string hash-table array  

The Sender With Largest Word Count problem on LeetCode is a fairly easy problem that can be solved using simple string manipulation techniques in Python. Here is the detailed solution for this problem:

Problem Statement:

Given a string sentence, which consists of lowercase/uppercase letters and spaces, identify the sender of the sentence who has the largest word count. Return the sender with the largest word count. If there is a tie between two or more senders, return the one that appears first in the sentence.

Example:

Input: sentence = "Hello my name is John and I like to play video games. My friend Mike also likes to play video games. How about you?" Output: "video"

Explanation: The word "video" appears twice in the sentence, and it belongs to both "John" and "Mike", but since "John" appears before "Mike" in the sentence, "video" is attributed to "John".

Solution:

Firstly, we need to split the given sentence into individual words, so that we can count the number of words for each sender. We can do this using the split() function in Python, which splits a string into a list of words based on a specified delimiter. In this case, we can use the space character " " as the delimiter to split the sentence into words.

Next, we need to create a dictionary to store the word count for each sender. We can do this by iterating over the list of words and checking if the word is a sender name or not. If it is a sender name, we update the word count for that sender in the dictionary.

Finally, we need to find the sender with the largest word count. We can do this by iterating over the dictionary and finding the sender with the maximum word count. If there is a tie, we return the sender that appears first in the sentence.

Here is the Python code for the above algorithm:

def findSender(sentence): # Split the sentence into words words = sentence.split()

# Create a dictionary to store the word count for each sender
wordCounts = {}

# Iterate over the list of words and update the word count for each sender
for i in range(len(words)):
    if words[i].isupper() and i+1<len(words) and words[i+1].islower():
        sender = words[i]
        if sender not in wordCounts:
            wordCounts[sender] = 0
        wordCounts[sender] += 1

# Find the sender with the maximum word count
maxCount = 0
maxSender = ""
for sender in wordCounts:
    count = wordCounts[sender]
    if count > maxCount:
        maxCount = count
        maxSender = sender
    elif count == maxCount:
        # If there is a tie, return the sender that appears first in the sentence
        maxSender = max(maxSender, sender)

return maxSender

Test the function on the example input

sentence = "Hello my name is John and I like to play video games. My friend Mike also likes to play video games. How about you?" print(findSender(sentence))

Output: "video"

Time Complexity:

The time complexity of this algorithm is O(n), where n is the number of words in the sentence. This is because we need to iterate over the list of words and perform constant time operations for each word.

Space Complexity:

The space complexity of this algorithm is O(k), where k is the number of senders in the sentence. This is because we need to store the word count for each sender in a dictionary, which can have a maximum of k keys.

Sender With Largest Word Count Solution Code

1