Number of Segments in a String LeetCode solution in JavaScript

1
var countSegments = function (s) {
2
let count = 0;
3

4
for (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 string
6
if (s[i] !== " " && (i === 0 || s[i - 1] === " ")) {
7
count++;
8
}
9
}
10

11
return count;
12
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0