Length of Last Word Algorithm
Share:
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Text
var lengthOfLastWord = function(s) {
const newS = s.trim().split(" ")
return newS[newS.length - 1].length
};
