Maximum Number of Words Found in Sentences

easy string counting

Problem

You are given an array of sentences, where each sentence is a string of words separated by single spaces with no leading or trailing spaces. Return the largest number of words that any single sentence in the array contains.

Inputsentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"]
Output6
The sentences have 5, 4, and 6 words. The maximum is 6.

def most_words_found(sentences):
    best = 0
    for s in sentences:
        words = s.count(' ') + 1
        if words > best:
            best = words
    return best
function mostWordsFound(sentences) {
  let best = 0;
  for (const s of sentences) {
    let words = 1;
    for (const c of s) if (c === ' ') words++;
    if (words > best) best = words;
  }
  return best;
}
class Solution {
    public int mostWordsFound(String[] sentences) {
        int best = 0;
        for (String s : sentences) {
            int words = 1;
            for (char c : s.toCharArray()) if (c == ' ') words++;
            if (words > best) best = words;
        }
        return best;
    }
}
int mostWordsFound(vector<string>& sentences) {
    int best = 0;
    for (const string& s : sentences) {
        int words = 1;
        for (char c : s) if (c == ' ') words++;
        if (words > best) best = words;
    }
    return best;
}
Time: O(total chars) Space: O(1)