# 434 Number of Segments in a String

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
```

**Time Complexity:** O(n) time and space

Not a great problem, in my opinion

```python
class Solution:
    def countSegments(self, s):
        """
        :type s: str
        :rtype: int
        """
        split_str = s.split()
        return len(split_str)


obj = Solution()
assert(obj.countSegments("kasjd asdj, adkja askjd") == 4)
assert(obj.countSegments("      ") == 0)
assert(obj.countSegments("") == 0)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/434-number-of-segments-in-a-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
