Watch 10 video solutions for Number of Segments in a String, a easy level problem involving String. This walkthrough by Nick White has 5,525 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: s = "Hello, my name is John" Output: 5 Explanation: The five segments are ["Hello,", "my", "name", "is", "John"]
Example 2:
Input: s = "Hello" Output: 1
Constraints:
0 <= s.length <= 300s consists of lowercase and uppercase English letters, digits, or one of the following characters "!@#$%^&*()_+-=',.:".s is ' '.Problem Overview: You receive a string that may contain words separated by one or more spaces. A segment is defined as a contiguous sequence of non-space characters. The task is to count how many such segments exist in the string.
Approach 1: Splitting Based on Spaces (O(n) time, O(n) space)
This approach uses the built-in split() function available in most languages. Splitting the string on spaces produces an array of tokens, where each token represents a potential word. Because multiple spaces can appear between words, you must filter out empty strings before counting. Internally, the runtime scans the entire string once to create substrings, which makes the time complexity O(n). The additional array of tokens requires O(n) auxiliary space.
This method is concise and readable. It works well in scripting or production environments where clarity matters more than micro-optimizations. Since the problem revolves around parsing a string, built-in splitting utilities are often the fastest way to express the solution.
Approach 2: Manual Counting with Iteration (O(n) time, O(1) space)
A more interview-focused approach scans the string character by character and counts the start of each segment. A new segment begins when the current character is not a space and either it is the first character in the string or the previous character is a space. Every time this condition appears, increment the counter. This detects transitions from space to non-space characters.
The algorithm performs a single linear pass over the string using a simple loop, giving O(n) time complexity. It only stores a counter and a few temporary variables, so the extra space is O(1). This technique is a classic pattern in string traversal problems where you detect boundaries between tokens without allocating additional data structures.
Recommended for interviews: Manual counting with iteration is typically what interviewers expect. It shows that you understand how segments are formed and can detect word boundaries during a single scan. The split-based solution demonstrates familiarity with language utilities, but the iterative approach proves stronger algorithmic reasoning and better space efficiency.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Splitting Based on Spaces | O(n) | O(n) | Quick implementation using built-in string split utilities |
| Manual Counting with Iteration | O(n) | O(1) | Preferred in interviews or memory-constrained scenarios |