A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
You are given an array of strings sentences
, where each sentences[i]
represents a single sentence.
Return the maximum number of words that appear in a single sentence.
Example 1:
Input: sentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"] Output: 6 Explanation: - The first sentence, "alice and bob love leetcode", has 5 words in total. - The second sentence, "i think so too", has 4 words in total. - The third sentence, "this is great thanks very much", has 6 words in total. Thus, the maximum number of words in a single sentence comes from the third sentence, which has 6 words.
Example 2:
Input: sentences = ["please wait", "continue to fight", "continue to win"] Output: 3 Explanation: It is possible that multiple sentences contain the same number of words. In this example, the second and third sentences (underlined) have the same number of words.
Constraints:
1 <= sentences.length <= 100
1 <= sentences[i].length <= 100
sentences[i]
consists only of lowercase English letters and ' '
only.sentences[i]
does not have leading or trailing spaces.sentences[i]
are separated by a single space.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for this problem is straightforward: we will count the words in each sentence and then compare these counts. Our goal is to find the sentence with the highest number of words by checking every sentence individually.
Here's how the algorithm would work step-by-step:
def maximum_number_of_words_found_in_sentences(sentences):
maximum_word_count = 0
for sentence in sentences:
# Split the sentence into words using space as the delimiter
words = sentence.split()
current_word_count = len(words)
# Update maximum_word_count if current_word_count is greater
if current_word_count > maximum_word_count:
maximum_word_count = current_word_count
return maximum_word_count
The goal is to find the sentence with the most words. We need to go through each sentence and count how many words it has. Finally, we return the largest word count we saw during this process.
Here's how the algorithm would work step-by-step:
def maximum_number_of_words_found_in_sentences(sentences):
max_words = 0
for sentence in sentences:
# Split the sentence into words using spaces as delimiters.
words = sentence.split()
number_of_words = len(words)
# Update the maximum word count if needed.
if number_of_words > max_words:
max_words = number_of_words
# We have now iterated through all sentences.
return max_words
Case | How to Handle |
---|---|
Null or empty input array | Return 0 immediately, as there are no sentences to process. |
Array containing only empty strings | Each empty string will have 0 words, so return 0. |
A single sentence in the array | The maximum number of words is simply the number of words in that sentence, so count and return. |
Very long sentences (close to maximum string length) | Ensure the word counting logic is efficient and avoids unnecessary memory allocation or string copies, potentially causing memory exhaustion. |
Sentences with leading/trailing spaces or multiple spaces between words | Trim leading/trailing spaces and handle multiple spaces between words correctly to count accurate words. |
Sentences with punctuation marks (periods, commas, etc.) within words | Define clear rules for how punctuation affects word separation (e.g., treat 'word.' as one word or two). |
Sentences containing only spaces | These sentences should be treated as having zero words, and should not cause errors in word counting logic. |
Maximum sized input array of very long sentences | The solution's space and time complexity should be considered for large datasets to ensure it doesn't exceed memory limits or take excessive time. |