[LeetCode] 434. Number of Segments in a String_Easy

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


Code
class Solution:
    def countSegments(self, s):
        return len(s.split())

猜你喜欢

转载自www.cnblogs.com/Johnsonxiong/p/9545673.html