Skip to main content
LeetCode 434, Easy. Topics: String. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:

Problem

Given a string s, return the number of segments in the string. A segment is defined to be a contiguous sequence of non-space characters.

Examples

Constraints

  • 0 <= s.length <= 300
  • s consists of lowercase and uppercase English letters, digits, or one of the following characters ”!@#$%^&*()_+-=’,.:”.
  • The only space character in s is ’ ’.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026