Python One-liner without split


  • 1

    Just increment count when we encounter a letter following a space.

        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.