https://leetcode.com/problems/length-of-last-word/
Solution 1
/**
* @param {string} s
* @return {number}
*/
var lengthOfLastWord = function (s) {
let start;
let end;
for (let i = s.length - 1; i >= 0; i--) {
if (s[i] !== ' ') {
start = i;
break;
}
}
for (let j = start; j >= 0; j--) {
if (s[j] === ' ') {
end = j;
break;
}
}
if (end === undefined) {
return start + 1;
}
return start - end;
};