GAZAR

Principal Engineer | Mentor

Length of Last Word Algorithm

Length of Last Word Algorithm

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.

var lengthOfLastWord = function(s) {
    const newS = s.trim().split(" ")
    return newS[newS.length - 1].length
};