Number of segments in a string

January 5, 2023

array-and-hashmap

Problem URL: Number of segments in a string

We will split the string by space, then count the number of non-empty string.

class Solution:
    def countSegments(self, s: str) -> int:
        return len(s.split())

Time complexity: O(n)
Space complexity: O(n)