Skip to content

Commit 71dfea4

Browse files
committed
finish 58
1 parent 41061a3 commit 71dfea4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

58. Length of Last Word.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 58. Length of Last Word
3+
*
4+
* Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
5+
*
6+
* If the last word does not exist, return 0.
7+
*
8+
* Note: A word is defined as a character sequence consists of non-space characters only.
9+
*
10+
* Example:
11+
*
12+
* Input: "Hello World"
13+
* Output: 5
14+
*/
15+
16+
/**
17+
* @param {string} s
18+
* @return {number}
19+
*/
20+
var lengthOfLastWord = function(s) {
21+
var str = s.trim();
22+
var len = str.length;
23+
var i = len - 1;
24+
while (i >= 0 && str[i] !== ' ') i--;
25+
return len - 1 - i;
26+
};

0 commit comments

Comments
 (0)