https://leetcode.com/problems/number-of-segments-in-a-string/
/**
* @param {string} s
* @return {number}
*/
var countSegments = function (s) {
if (s.length === 0) {
return 0;
}
let result = s[0] !== ' ' ? 1 : 0;
for (let i = 0; i < s.length; i++) {
if (s[i - 1] === ' ' && s[i] !== ' ') {
result++;
}
}
return result;
};
Runtime: 77 ms, faster than 76.86% of JavaScript online submissions for Number of Segments in a String.
Memory Usage: 42 MB, less than 30.33% of JavaScript online submissions for Number of Segments in a String.