Explanation

588.Count the Words in a Sentence

Easy
Given a sentence text made of letters and spaces, return how many words it contains. Words are separated by one or more spaces, and there may be spaces at the start or the end. Counting words looks like "count the spaces and add one", but that breaks as soon as spaces are doubled or appear at the edges. The reliable idea is to count word boundariesWord BoundaryThe point where a word begins or ends — e.g. a letter that follows a space or the start of the text. — the places where a word begins. Splitting on spaces works too, as long as the empty pieces created by extra spaces are thrown away.

Example 1:

Input: text = "the quick brown fox"

Output: 4

Example 2:

Input: text = " many spaces here"

Output: 3

+ 5 hidden test cases run on Submit.

Constraints:

  • ●1 ≤ text.length ≤ 1000
  • ●text contains English letters and spaces only

text =

the quick brown fox