EasyNo. 2114

Maximum Number of Words Found in Sentences — LeetCode 2114 Python Solution

Pattern
Hash Map
Topics
Array, String
Reading time
2 min

The 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.

Example

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.

Python solution

PythonSolution
class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        return 1 + max(s.count(' ') for s in sentences)

Complexity

Time
O(L)where L is the total length of all strings in the array `sentences`
Space · auxiliary
O(1)

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2114. Maximum Number of Words Found in Sentences is filed here because the reference solution below belongs to the algorithm family this hub collects, even though its LeetCode tags point elsewhere.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2114. Maximum Number of Words Found in Sentences?

LeetCode 2114. Maximum Number of Words Found in Sentences is rated Easy on LeetCode.

What is the time complexity of LeetCode 2114. Maximum Number of Words Found in Sentences?

The Python solution on this page runs in O(L), where L is the total length of all strings in the array `sentences`.

What is the space complexity of LeetCode 2114. Maximum Number of Words Found in Sentences?

The Python solution on this page uses O(1) auxiliary space.

What topics does LeetCode 2114. Maximum Number of Words Found in Sentences cover?

LeetCode 2114. Maximum Number of Words Found in Sentences is tagged Array and String on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.