one pass python solution


  • 0
    F
    def countSegments(self, s):
            res = 0
            for i in range(len(s)):
                if s[i] != ' ' and (i == 0 or s[i - 1] == ' '):
                    res += 1
            return res
    

    or one line:

        def countSegments(self, s):
            return sum([s[i] != ' ' and (i == 0 or s[i - 1] == ' ') for i in range(len(s))])
    

Log in to reply
 

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