Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.
If the last word does not exist, return 0.
Note: A word is defined as a maximal substring consisting of non-space characters only.
給定一段文字,各單字以空格分開,找出最後一個單字的長度,
如果最後一個單字不存在,則回傳0。
Input: "Hello World"
Output: 5
先將各單字分拆為陣列,
再找出最後一個元素的長度。
var lengthOfLastWord = function(s) {
let strAry = s.trim().split(' ');
return strAry[strAry.length - 1].length;
};