30:00
Given a string s, return the number of segments in it. A segment is a maximal run of consecutive characters that are not spaces.
s
0≤s.length≤3000 \leq s.\text{length} \leq 3000≤s.length≤300
s consists of lowercase and uppercase English letters, digits, or one of !@#$%^&*()_+-=',.:.
!@#$%^&*()_+-=',.:
The only space character in s is ' '.
' '
Run code to see results.
s: a flag marks each left edge, where a non-space follows a space
A segment begins exactly where a non-space character follows a space (or the start of the string). Count those left edges.
segments
count 0
Count the maximal runs of non-space characters in s. For "Hello, my name is John" the answer is 5. Spaces can repeat and can lead or trail, and s may be empty.