#434

Number of Segments in a String

pupil · 565 · lc easy +29 · verified · 37% accepted · 904 likes · top 15%

play →

Description

Given a string s, return the number of segments it contains. A segment is a maximal run of non-whitespace characters. Any amount of surrounding or separating whitespace does not count as part of a segment.

Example 1:

Input: s = "Hello, my name is John"
Output: 5
Explanation: The five segments are ["Hello,", "my", "name", "is", "John"]

Example 2:

Input: s = "Hello"
Output: 1

Code

1
2
3