Maximum Number of Words Found in Sentences (LeetCode 2114): Solution and Explanation
Each string in the array is a sentence of words separated by single spaces. Return the largest number of words in any one sentence.
Intuition
Because words are separated by exactly one space and nothing sits at either end, every space sits between two words. A sentence with k spaces therefore has k + 1 words.
That turns word counting into character counting, with no need to build a list of words.
The answer is the maximum of those counts, which a single running variable tracks as you go.
Approach: Count spaces, keep the maximum
- Set
best = 0. - For each sentence, count its spaces and add 1 to get the number of words.
- Update
best = max(best, words). - Return
best.
Step-by-step example
sentences = ["alice and bob love leetcode","i think so too","this is great thanks very much"]
- Sentence 0 has 4 spaces, so 5 words. best = 5.
- Sentence 1 has 3 spaces, so 4 words. 4 is not more than 5, best stays 5.
- Sentence 2 has 5 spaces, so 6 words. best = 6.
- Return 6.
Code: Python, Java, C++
1class Solution:
2 def mostWordsFound(self, sentences: List[str]) -> int:
3 best = 0
4 for sentence in sentences:
5 words = sentence.count(' ') + 1
6 best = max(best, words)
7 return best1class Solution {
2 public int mostWordsFound(String[] sentences) {
3 int best = 0;
4 for (String sentence : sentences) {
5 int words = sentence.split(" ").length;
6 best = Math.max(best, words);
7 }
8 return best;
9 }
10}1class Solution {
2public:
3 int mostWordsFound(vector<string>& sentences) {
4 int best = 0;
5 for (const string& sentence : sentences) {
6 int words = count(sentence.begin(), sentence.end(), ' ') + 1;
7 best = max(best, words);
8 }
9 return best;
10 }
11};Time and space complexity
O(L)
L is the total number of characters across all sentences; each is looked at once.
O(1)
Counting spaces needs no extra storage. Splitting into words would use O(length) per sentence.
Other approaches
Split each sentence (time O(L), space O(max sentence length))
Split on spaces and take the length of the resulting list. Clear and short, but it allocates a list of words for every sentence.
Count word starts (time O(L), space O(1))
Count positions where a letter follows a space or starts the string. This version also works when spaces can repeat or appear at the ends.
Common mistakes
- Forgetting the + 1: the number of spaces is one less than the number of words.
- Initializing best to the first sentence's count and then skipping it in the loop, or reading sentences[0] without checking the list.
- Using a split that collapses whitespace in one language and not in another when porting the solution. With single spaces both agree, but it matters if the input rules change.
Frequently asked questions
Why does counting spaces work?
Each space separates two adjacent words, and there are no spaces at the start or end. So n words always come with exactly n - 1 spaces.
Is splitting slower than counting?
Both are linear in the length of the text. Splitting allocates strings, so counting is a little lighter, but either is fine for these limits.
What if two sentences tie?
The problem only asks for the count, so ties do not matter: the maximum is the same either way.