Introduction Link to heading

Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.

Please note that the string does not contain any non-printable characters.

Example:

Input: "Hello, my name is John"
Output: 5

Solution Link to heading

The trick here is that strings.Split function returns also empty tokens, that we need to escape. Therefore, I am using strings.FieldsFunc

func countSegments(s string) int {
    return len(strings.FieldsFunc(s, func(c rune) bool {
            return c == ' '
    }))
}

Performance Link to heading

Nothing is to improve.

Runtime: 0 ms, faster than 100.00% of Go online submissions for Number of Segments in a String.
Memory Usage: 2 MB, less than 100.00% of Go online submissions for Number of Segments in a String.