dsa · easy
Number of Segments in a String
A segment is a maximal run of non-space characters. Spaces only separate segments; they do not count as segments themselves. Return how many segments s contains.
Example
s = "go, now" has two runs: "go," and "now" → 2. The comma stays attached to the first run because it is not a space.
s = " padded text " has two runs (padded, text) even though spaces pile up on both sides → 2.
s = "" and s = " " have no non-space run → 0.
## Arguments - s — string that may mix words, punctuation, and spaces
Constraints
0 <= s.length <= 300 s consists of English letters, digits, spaces, and punctuation Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: "go, now" Expected: 2
Example 2
Input: " padded text " Expected: 2
Example 3
Input: "" Expected: 0