Fast Python solution

  • 0

    Accidentally, I found the following solution is faster that the most popular one:
    return len(s.split())

    The only possibility that I can image is that the default parameter of delimiters includes a lot of possible chars to be checked. In this case, only space is possible. To provide this explicitly makes it faster.

    class Solution(object):
        def countSegments(self, s):
            :type s: str
            :rtype: int
            return len(filter(bool, s.split(' ')))

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.