Member-only story

Leetcode 2114: Maximum Number of Words Found in Sentences

Pierre-Marie Poitevin
2 min readJan 3, 2022

--

In this problem, we are given a list of sentences, and we should return the maximum number of words in these sentences.

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.

We are given some examples:

Examples

We can notice that the number of words is simply the 1 + #' ', and so we just need to count the number of spaces in each sentence, and then return the max.

One edge case is the empty string, in that case the number of words is 0.

Jamboard with thoughts about problem

This is the implementation in C:

int countWords(char* sentence) {
int n = strlen(sentence);
if (n == 0) {
return 0;
}
int countEmptySpace = 0;
for (int i = 0; i < n; i++) {
if (sentence[i] == ' ') {
countEmptySpace++;
}
}
return countEmptySpace + 1;
}

int mostWordsFound(char ** sentences, int sentencesSize){
int maxWords = 0;
for (int i = 0…

--

--

Pierre-Marie Poitevin
Pierre-Marie Poitevin

No responses yet