Python, beats 80%


  • 0
    S
    def countSegments(self, s):
            count = 0
            length = len(s)
            if length == 0:
                return count
                
            i = 0
            while i < length and s[i] == ' ':
                i += 1
            
            while i < length:
                while i < length and s[i] != ' ':
                    i += 1
                count += 1
                    
                while i < length and s[i] == ' ':
                    i += 1
    
            return count
    

Log in to reply
 

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