Leetcode 58 - Length of last word

Note:

  • Just use trim to remove leading and trailing spaces.

Question:

Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string.

A word is a maximal substring consisting of non-space characters only.

Example:

1
2
3
Input: s = "   fly me   to   the moon  "
Output: 4
Explanation: The last word is "moon" with length 4.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @param {string} s
* @return {number}
*/
var lengthOfLastWord = function(s) {
s = s.trim();
let res = 0;
for (let i = s.length - 1; i >= 0; i--) {
if (s[i] === ' ') break;
res++;
}
return res;
};