Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"Hello, my name is John"
"Hello"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #434: Number of Segments in a String
class Solution {
public int countSegments(String s) {
int ans = 0;
for (String t : s.split(" ")) {
if (!"".equals(t)) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #434: Number of Segments in a String
func countSegments(s string) int {
ans := 0
for _, t := range strings.Split(s, " ") {
if len(t) > 0 {
ans++
}
}
return ans
}
# Accepted solution for LeetCode #434: Number of Segments in a String
class Solution:
def countSegments(self, s: str) -> int:
return len(s.split())
// Accepted solution for LeetCode #434: Number of Segments in a String
struct Solution;
impl Solution {
fn count_segments(s: String) -> i32 {
s.split_whitespace().count() as i32
}
}
#[test]
fn test() {
let s = "Hello, my name is John".to_string();
assert_eq!(Solution::count_segments(s), 5);
}
// Accepted solution for LeetCode #434: Number of Segments in a String
function countSegments(s: string): number {
return s.split(/\s+/).filter(Boolean).length;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.