LeetCode Problem Workspace

Maximum Number of Words Found in Sentences

Find the maximum number of words in a single sentence from an array of sentences using array and string processing techniques.

category

2

Topics

code_blocks

7

Code langs

hub

3

Related

Practice Focus

Easy · Array plus String

bolt

Answer-first summary

Find the maximum number of words in a single sentence from an array of sentences using array and string processing techniques.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array plus String

Try AiBox Copilotarrow_forward

The goal is to scan each sentence individually and count words by identifying spaces. By keeping track of the maximum count, you can quickly determine which sentence contains the most words. This problem combines array traversal and string splitting, emphasizing careful handling of spaces without leading or trailing errors.

Problem Statement

You are given an array of strings called sentences, where each string represents a single sentence consisting of words separated by single spaces with no leading or trailing spaces. Your task is to determine how many words appear in the sentence that contains the most words.

Return an integer representing the maximum number of words in any single sentence. Each sentence will only contain lowercase English letters and spaces, and all words are separated by exactly one space.

Examples

Example 1

Input: sentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"]

Output: 6

  • 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

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.
  • All the words in sentences[i] are separated by a single space.

Solution Approach

Iterate and Count Spaces

Loop through each sentence in the array, count the number of spaces, and add one to get the total words in that sentence. Keep track of the maximum word count seen so far.

Use String Split

For each sentence, split it by spaces into an array of words. The length of the resulting array is the number of words. Update the maximum count if this is larger than the current maximum.

Combine Array and String Processing

Leverage array traversal to process each sentence and string methods to count words efficiently. This approach handles any number of sentences and ensures correct word counting without extra trimming or checks.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n*m) where n is the number of sentences and m is the average length of a sentence. Space complexity is O(1) if counting manually or O(m) if splitting strings into arrays.

What Interviewers Usually Probe

  • Expecting an array traversal with string manipulation to count words.
  • Looking for edge case handling like single-word sentences and uniform spaces.
  • Evaluates familiarity with split, length, and iterative maximum updates.

Common Pitfalls or Variants

Common pitfalls

  • Counting spaces incorrectly when multiple spaces exist, although constraints prevent this.
  • Forgetting to add one to the space count to get the total words.
  • Assuming empty sentences or trimming is needed, which violates the problem constraints.

Follow-up variants

  • Return the index of the sentence with the maximum words instead of the count.
  • Count words ignoring punctuation or non-letter characters in sentences.
  • Handle sentences with varying spacing and leading/trailing spaces.

FAQ

What is the main approach to solve Maximum Number of Words Found in Sentences?

Iterate through each sentence, count words using spaces, and track the maximum number found in any sentence.

Can I use split method to count words?

Yes, splitting each sentence by spaces gives an array whose length is the word count.

How do I handle single-word sentences?

A single-word sentence has zero spaces, so adding one gives the correct count of one.

What is the time complexity for this problem?

Time complexity is O(n*m) where n is the number of sentences and m is the average sentence length.

Do I need to worry about leading or trailing spaces?

No, constraints guarantee sentences have no leading or trailing spaces and words are separated by exactly one space.

terminal

Solution

Solution 1: Space Counting

We iterate through the array `sentences`. For each sentence, we count the number of spaces, then the number of words is the number of spaces plus $1$. Finally, we return the maximum number of words.

1
2
3
class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        return 1 + max(s.count(' ') for s in sentences)
Maximum Number of Words Found in Sentences Solution: Array plus String | LeetCode #2114 Easy