This one is one of the easy problems on LeetCode.
To solve this problem, mainly you have to know how to split a sentence based on spaces and count array elements. If you know the basics of Python, this will not be a big deal for you. You can solve this problem.
I am not going to explain the details. See the problem details. It’s simple, and I hope you got the idea of it.
We would rather go to the coding solution. Its code is also simple. I hope you will not have difficulty in understanding.
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: max_number = 0 for sentence in sentences: words = sentence.split(' ') count = len(words) if count > max_number: max_number = count return max_number
One line solution
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: return max([len(sentence.split()) for sentence in sentences])
I hope you got the idea of the solution. There is more than one solution to a problem. And it’s a good practice to try those solutions also.
If you found this solution helpful, that means a lot to me.
Happy Learning.
Happy Coding.