Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.spa
For example,code
Input: "Hello, my name is John"
blog
Output: 5
string
public int countSegments(String s) { String trimmed = s.trim(); if (trimmed.length() == 0) return 0; else return trimmed.split("\\s+").length; }