Skip to content

Latest commit

 

History

History
57 lines (48 loc) · 1.46 KB

2114.-maximum-number-of-words-found-in-sentences.md

File metadata and controls

57 lines (48 loc) · 1.46 KB

2114. Maximum Number of Words Found in Sentences

  • Easy
  • 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 the maximum number of words that appear in a single sentence.

Solution (Python)

class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        maxi=0
        for sentence in sentences:
            count=0
            for char in sentence:
                if char==" ":
                #use an space to separate different words
                
                    count+=1
            #the number of spaces will always be one less than the total number of words
            count+=1
            if count>maxi:
                maxi=count
        return maxi

Solution (C)

int mostWordsFound(char ** sentences, int sentencesSize){
    int max=0;
    for (int i=0;i<sentencesSize;i++)
    {
        int length=strlen(sentences[i]);
        char* sentence=sentences[i];
        int count=1;
        for (int j=0;j<length;j++)
        {
            if (sentence[j]==32)
            {
                count+=1;
            }
        }
        if (count>max)
        {
            max=count;
        }
    }

    return max;
}