Watch 10 video solutions for Maximum Number of Words Found in Sentences, a easy level problem involving Array, String. This walkthrough by Erudite CS has 4,119 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |