Skip to content

Latest commit

 

History

History
75 lines (50 loc) · 3.57 KB

029.md

File metadata and controls

75 lines (50 loc) · 3.57 KB

Difficulty: 🟢 Easy

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.

Examples:

Example 1:

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.
- The second sentence, "i think so too", has 4 words in total.
- The third sentence, "this is great thanks very much", has 6 words in total.
Thus, the maximum number of words in a single sentence comes from the third sentence, which has 6 words.

Example 2:

Input: sentences = ["please wait","continue to fight","continue to win"]
Output: 3
Explanation: It is possible that multiple sentences contain the same number of words.
In this example, the second and third sentences (underlined) have the same number of words.

Constraints:

  • 1 <= sentences.length <= 100
  • 1 <= sentences[i].length <= 100
  • sentences[i] consists only of lowercase English letters and ' ' only.
  • sentences[i] does not have leading or trailing spaces.
  • All the words in sentences[i] are separated by a single space.

Solutions

O(n * m) solution in Python3

class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        return max(len(sentence.split(" ")) for sentence in sentences)

The given solution finds the maximum number of words that appear in a single sentence by splitting each sentence into words and finding the length of the resulting word list. It uses a generator expression and the max() function to determine the maximum length.

The algorithm works as follows:

  1. Use a generator expression to iterate through each sentence in the sentences array.
  2. Split each sentence into words using the split() method with the space character as the delimiter.
  3. Use the len() function to calculate the length of the resulting word list for each sentence.
  4. Pass the generator expression to the max() function to find the maximum length among all sentences.
  5. Return the maximum length, which represents the maximum number of words found in a single sentence.

The algorithm splits each sentence into words and calculates the length of the resulting word list for each sentence. It then determines the maximum length among all sentences.

Analysis of Time and Space Complexity

The time complexity of this algorithm is O(n * m), where n is the number of sentences and m is the maximum number of words in a single sentence. The algorithm iterates through each sentence and splits it into words, which takes O(m) time. Since the maximum number of words can be at most 100, the overall time complexity is still efficient.

The space complexity of the algorithm is O(1) as it does not use any extra space that grows with the input size.

Summary

The given solution finds the maximum number of words that appear in a single sentence by splitting each sentence into words and calculating the length of the resulting word list. It has a time complexity of O(n * m) and a space complexity of O(1), making it an efficient solution for the problem at hand.

NB: If you want to get community points please suggest solutions in other languages as merge requests.