Skip to content

2114. Maximum Number of Words Found in Sentences 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  int mostWordsFound(vector<string>& sentences) {
    long maxSpaceCount = 0;

    for (const auto& s : sentences)
      maxSpaceCount = max(maxSpaceCount, count(begin(s), end(s), ' '));

    return maxSpaceCount + 1;
  }
};
1
2
3
4
5
6
7
8
class Solution {
  public int mostWordsFound(String[] sentences) {
    return 1 + Stream.of(sentences)
                   .mapToInt(s -> (int) s.chars().filter(c -> c == ' ').count())
                   .max()
                   .getAsInt();
  }
}
1
2
3
class Solution:
  def mostWordsFound(self, sentences: List[str]) -> int:
    return max(s.count(' ') for s in sentences) + 1
Back to top