#2114

Maximum Number of Words Found in Sentences

Easy String scanning

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Each sentence is words separated by single spaces. Return the largest word count among all sentences.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: which sentence has the most words?

Why this works: words are separated by exactly one space, so words = spaces + 1. Counting spaces avoids building arrays with split().

Example: ["alice and bob love leetcode", "i think so too", "this is great thanks very much"]

4 spaces → 5 words3 spaces → 4 words5 spaces → 6 words → answer 6

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting the +1 — three words have only two spaces.