We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 41061a3 commit 71dfea4Copy full SHA for 71dfea4
58. Length of Last Word.js
@@ -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