Given a string s consists of some words separated by spaces, return the length of the last word in the string. If the last word does not exist, return 0.
A word is a maximal substring consisting of non-space characters only.
Example 1:
Input: s = "Hello World"
Output: 5
Example 2:
Input: s = " "
Output: 0
Constraints:
1 <= s.length <= 104
s consists of only English letters and spaces ' '.
var lengthOfLastWord = function(s) {
const strToArr = s.trim().split(' ')
return strToArr[strToArr.length-1].length
};
Runtime: 68 ms, faster than 92.32% of JavaScript online submissions for Length of Last Word.
Memory Usage: 39 MB, less than 18.46% of JavaScript online submissions for Length of Last Word