15. Count Words
EasyStrings~7 min
Return how many words a sentence contains. Words are separated by one or more spaces.
Leading and trailing spaces should not create phantom words, and a string of only spaces contains zero words.
Examples
Example 1
- Input:
- sentence = "hello world"
- Output:
- 2
Example 2
- Input:
- sentence = " spaced out "
- Output:
- 2
- Why:
- Extra spaces are ignored.
Example 3
- Input:
- sentence = " "
- Output:
- 0
Constraints
0 <= sentence.length <= 2000Only the space character separates words.