Maximum Number of Words Found in Sentences

To find the maximum number of words in a sentence from a list of sentences, you need to split each sentence into words and count the number of words. The split function in Python can be used for this purpose. It splits a string into a list where each word is a list item. By counting the length of the list, you can find out the number of words in each sentence.

1
2
3
4
5
6
7
class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        max_words = 0
        for sentence in sentences:
            words = sentence.split(' ')
            max_words = max(max_words, len(words))
        return max_words

In this function, for each sentence in the sentences list, we split the sentence into words using the split function. We then compare the number of words in the current sentence with the maximum number of words found so far, and update the maximum if necessary. Finally, we return the maximum number of words found.