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

1
2
3
4
5
6
7
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

1
2
3
4
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 <= 100
  • 1 <= sentences[i].length <= 100
  • sentences[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

  1. For each sentence in the input list, split the sentence by spaces to get the words.
  2. Count the number of words in each sentence.
  3. Track the maximum word count found.
  4. Return the maximum.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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
    }
}
1
2
3
4
5
6
7
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
1
2
3
4
5
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()
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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) — Where n is the number of sentences and m is the average length of a sentence.
  • 🧺 Space complexity: O(1) — Only a few variables are used for counting.