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 <= 1001 <= sentences[i].length <= 100sentences[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.Problem Overview: You receive an array of sentences where each sentence contains words separated by single spaces. The task is simple: determine the maximum number of words that appear in any single sentence.
The key observation is that the number of words in a sentence equals the number of spaces plus one. Every space separates two words, so counting spaces becomes the fastest way to compute the word count. Since each sentence is processed independently, the goal is to scan each string and track the maximum word count seen so far.
Approach 1: Counting Spaces Method (Time: O(n), Space: O(1))
This approach relies on the observation that words are separated by exactly one space. Iterate through each sentence in the array and count how many spaces it contains using a simple loop or built‑in string operation. The number of words equals spaces + 1. Maintain a variable that stores the maximum word count encountered while iterating through the list of sentences.
The algorithm scans every character once across all sentences, so the total time complexity is O(n), where n is the total number of characters across the input array. Space complexity remains O(1) because only a few integer counters are used. This is the most direct solution and works well for problems involving simple string processing inside an array.
Approach 2: Iterative Character Count Method (Time: O(n), Space: O(1))
This method performs a full character scan for each sentence while explicitly counting words. Start with a word counter initialized to 1 for every non-empty sentence, then iterate through each character. Every time you encounter a space character (' '), increment the word counter. After processing the sentence, compare the result with the global maximum and update it if necessary.
The complexity remains O(n) because each character is visited exactly once. Space usage stays O(1). While this approach produces the same result as the previous one, it makes the counting logic explicit and is sometimes easier to adapt if the delimiter rules change (for example, handling multiple spaces or different separators).
Recommended for interviews: The Counting Spaces Method is the expected solution. Interviewers want to see that you recognize the relationship between spaces and word counts instead of splitting strings or allocating extra arrays. A brute approach using split() also works but increases memory usage to O(w) where w is the number of words. Demonstrating the space-count insight shows strong attention to problem constraints and efficient string traversal.
This approach involves counting the number of spaces in each sentence. The number of words in a sentence will be the number of spaces plus one since words are separated by a single space.
In this Python solution, we use list comprehension to split each sentence into words using the 'split()' method, which splits a string by its spaces. Then, we use Python's built-in 'max()' function to find the maximum length of the lists generated.
Time Complexity: O(n * m), where n is the number of sentences and m is the longest sentence length in terms of words.
Space Complexity: O(1) additional space apart from input data.
This method iteratively goes through each character of each sentence. It counts words by detecting spaces and updating the word count accordingly. This approach bypasses the need to use in-built functions like split.
This solution iterates through each sentence and subsequently each character. Every time a space is encountered, it increases the word count. It keeps track of the maximum word count found.
Python
JavaScript
Time Complexity: O(n * m), where n is the number of sentences and m is the length of the longest sentence.
Space Complexity: O(1) since no additional space is allocated.
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.
The time complexity is O(L), where L is the total length of all strings in the array sentences. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Counting Spaces Method | Time Complexity: O(n * m), where n is the number of sentences and m is the longest sentence length in terms of words. |
| Iterative Character Count Method | Time Complexity: O(n * m), where n is the number of sentences and m is the length of the longest sentence. |
| Space Counting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Counting Spaces Method | O(n) | O(1) | Best general solution when sentences use single spaces between words |
| Iterative Character Count | O(n) | O(1) | Useful when implementing explicit character-level parsing or adapting to custom delimiters |
Leetcode 2114 - Maximum Number of Words Found in Sentences • Erudite CS • 4,119 views views
Watch 9 more video solutions →Practice Maximum Number of Words Found in Sentences with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor