Back to all solutions

#434 - Number of Segments in a String

Problem Description

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.

Solution

/**
 * @param {string} s
 * @return {number}
 */
var countSegments = function(s) {
  return s.split(/\s+/).filter(s => s).length;
};