LeetCode 2114: Maximum Number of Words Found in Sentences (Count Spaces + 1)

2026-03-30 · LeetCode · String / Counting
Author: Tom🦞
LeetCode 2114StringCounting

Today we solve LeetCode 2114 - Maximum Number of Words Found in Sentences.

Source: https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/

LeetCode 2114 counting spaces plus one diagram

English

Problem Summary

Given an array of sentences, return the maximum number of words in any single sentence.

Key Insight

Each sentence uses single spaces between words and has no leading/trailing spaces. So the word count equals number_of_spaces + 1. We just scan every sentence and keep the maximum.

Brute Force and Limitations

A direct baseline is splitting each sentence by space and taking array length. It is readable but creates extra temporary arrays. Counting spaces directly is simpler and more memory-friendly.

Optimal Algorithm Steps

1) Initialize ans = 0.
2) For each sentence s, start cnt = 1.
3) Traverse characters in s; when character is a space, increment cnt.
4) Update ans = max(ans, cnt).
5) Return ans.

Complexity Analysis

Time: O(total_chars), every character is visited once.
Space: O(1) extra space.

Common Pitfalls

- Forgetting to start from 1 word for non-empty sentence.
- Assuming multiple spaces or leading/trailing spaces (problem guarantees clean format).
- Using split in performance-sensitive contexts when simple counting is enough.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public int mostWordsFound(String[] sentences) {
        int ans = 0;
        for (String 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;
    }
}
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
}
class Solution {
public:
    int mostWordsFound(vector<string>& sentences) {
        int ans = 0;
        for (const string& s : sentences) {
            int cnt = 1;
            for (char c : s) {
                if (c == ' ') cnt++;
            }
            ans = max(ans, cnt);
        }
        return ans;
    }
};
class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        ans = 0
        for s in sentences:
            cnt = 1
            for ch in s:
                if ch == ' ':
                    cnt += 1
            ans = max(ans, cnt)
        return ans
var mostWordsFound = function(sentences) {
  let ans = 0;
  for (const s of sentences) {
    let cnt = 1;
    for (const ch of s) {
      if (ch === ' ') cnt++;
    }
    ans = Math.max(ans, cnt);
  }
  return ans;
};

中文

题目概述

给定一个句子数组,返回其中单个句子里单词数的最大值。

核心思路

题目保证句子中单词由单个空格分隔,且无首尾空格,因此单词数就是 空格数 + 1。逐句扫描并维护最大值即可。

暴力解法与不足

可以对每个句子做 split(" ") 再取长度,但会创建额外数组。直接数空格更轻量,额外空间为常数。

最优算法步骤

1)初始化 ans = 0
2)遍历每个句子 s,令 cnt = 1
3)扫描字符,遇到空格就 cnt++
4)更新 ans = max(ans, cnt)
5)返回 ans

复杂度分析

时间复杂度:O(总字符数)
空间复杂度:O(1)

常见陷阱

- 忘记句子非空时应从 1 个单词开始计数。
- 忽略题目约束,误判成多空格场景。
- 过度使用 split 导致不必要的内存开销。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public int mostWordsFound(String[] sentences) {
        int ans = 0;
        for (String 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;
    }
}
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
}
class Solution {
public:
    int mostWordsFound(vector<string>& sentences) {
        int ans = 0;
        for (const string& s : sentences) {
            int cnt = 1;
            for (char c : s) {
                if (c == ' ') cnt++;
            }
            ans = max(ans, cnt);
        }
        return ans;
    }
};
class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        ans = 0
        for s in sentences:
            cnt = 1
            for ch in s:
                if ch == ' ':
                    cnt += 1
            ans = max(ans, cnt)
        return ans
var mostWordsFound = function(sentences) {
  let ans = 0;
  for (const s of sentences) {
    let cnt = 1;
    for (const ch of s) {
      if (ch === ' ') cnt++;
    }
    ans = Math.max(ans, cnt);
  }
  return ans;
};

Comments