Maximum Number of Words Found in Sentences
EasyUpdated: Aug 2, 2025
Practice on:
Problem
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 themaximum number of words that appear in a single sentence.
Examples
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.- All the words in
sentences[i]are separated by a single space.
Solution
Method 1 – Simple Split and Count
Intuition
Each sentence is a string of words separated by spaces. The number of words in a sentence is the number of spaces plus one. We can split each sentence by spaces and count the words, then return the maximum count.
Approach
- For each sentence in the input list, split the sentence by spaces to get the words.
- Count the number of words in each sentence.
- Track the maximum word count found.
- Return the maximum.
Code
C++
class Solution {
public:
int mostWordsFound(vector<string>& sentences) {
int ans = 0;
for (auto& s : sentences) {
int cnt = 1;
for (char c : s) if (c == ' ') cnt++;
ans = max(ans, cnt);
}
return ans;
}
};
Go
func mostWordsFound(sentences []string) int {
ans := 0
for _, s := range sentences {
cnt := 1
for i := 0; i < len(s); i++ {
if s[i] == ' ' {
cnt++
}
}
if cnt > ans {
ans = cnt
}
}
return ans
}
Java
class Solution {
public int mostWordsFound(String[] sentences) {
int ans = 0;
for (String s : sentences) {
int cnt = 1;
for (char c : s.toCharArray()) if (c == ' ') cnt++;
ans = Math.max(ans, cnt);
}
return ans;
}
}
Kotlin
class Solution {
fun mostWordsFound(sentences: Array<String>): Int {
var ans = 0
for (s in sentences) {
var cnt = 1
for (c in s) if (c == ' ') cnt++
ans = maxOf(ans, cnt)
}
return ans
}
}
Python
class Solution:
def mostWordsFound(self, sentences: list[str]) -> int:
ans = 0
for s in sentences:
cnt = s.count(' ') + 1
ans = max(ans, cnt)
return ans
Rust
impl Solution {
pub fn most_words_found(sentences: Vec<String>) -> i32 {
sentences.iter().map(|s| s.chars().filter(|&c| c == ' ').count() as i32 + 1).max().unwrap()
}
}
TypeScript
class Solution {
mostWordsFound(sentences: string[]): number {
let ans = 0;
for (const s of sentences) {
const cnt = s.split(' ').length;
ans = Math.max(ans, cnt);
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(n * m)— Wherenis the number of sentences andmis the average length of a sentence. - 🧺 Space complexity:
O(1)— Only a few variables are used for counting.