LeetCode 434. Number of Segments in a String

Problem Statement

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

For example,

Input: "Hello, my name is John"

Output: 5

Solution

class Solution(object):
    def countSegments(self, s):
        """ :type s: str :rtype: int """
        return len(s.split())
点赞