Number of Segments in a String

May 20, 2019

Introduction

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

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

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.

comments powered by Disqus

Do you want to know me more private?→Click!