Number of Segments in a String LeetCode solution in JavaScript
1var countSegments = function (s) {2let count = 0;34for (let i = 0; i < s.length; i++) {5// Check if the current character is a non-space character and the previous character is a space or the start of the string6if (s[i] !== " " && (i === 0 || s[i - 1] === " ")) {7count++;8}9}1011return count;12};