Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
["alice and bob love leetcode","i think so too","this is great thanks very much"]
["please wait","continue to fight","continue to win"]
number-of-valid-words-in-a-sentence)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2114: Maximum Number of Words Found in Sentences
class Solution {
public int mostWordsFound(String[] sentences) {
int ans = 0;
for (var s : sentences) {
int cnt = 1;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == ' ') {
++cnt;
}
}
ans = Math.max(ans, cnt);
}
return ans;
}
}
// Accepted solution for LeetCode #2114: Maximum Number of Words Found in Sentences
func mostWordsFound(sentences []string) (ans int) {
for _, s := range sentences {
cnt := 1 + strings.Count(s, " ")
if ans < cnt {
ans = cnt
}
}
return
}
# Accepted solution for LeetCode #2114: Maximum Number of Words Found in Sentences
class Solution:
def mostWordsFound(self, sentences: List[str]) -> int:
return 1 + max(s.count(' ') for s in sentences)
// Accepted solution for LeetCode #2114: Maximum Number of Words Found in Sentences
impl Solution {
pub fn most_words_found(sentences: Vec<String>) -> i32 {
let mut ans = 0;
for s in sentences.iter() {
let mut count = 1;
for c in s.as_bytes() {
if *c == b' ' {
count += 1;
}
}
ans = ans.max(count);
}
ans
}
}
// Accepted solution for LeetCode #2114: Maximum Number of Words Found in Sentences
function mostWordsFound(sentences: string[]): number {
return sentences.reduce(
(r, s) =>
Math.max(
r,
[...s].reduce((r, c) => r + (c === ' ' ? 1 : 0), 1),
),
0,
);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.