Maximum Number of Words Found in Sentences — LeetCode 2114 Python Solution
- Problem
- #2114
- Pattern
- Hash Map
- Reading time
- 2 min
- Source
- leetcode.com
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
class Solution:
def mostWordsFound(self, sentences: List[str]) -> int:
return 1 + max(s.count(' ') for s in sentences)Complexity
| Measure | Complexity |
|---|---|
| Time | O(L), where L is the total length of all strings in the array `sentences` |
| Space | O(1) auxiliary |
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.