LeetCode #58: Length of Last Word

Melissa Guachun
3 min readDec 21, 2021
Photo by Joshua Hoehne on Unsplash

As I slowly dip my toes into the great abyss of the Big O and binary searches, I am (again) humbled by LeetCode’s pattern for seemingly short answers for intimidating prompts. But nonetheless, this question reminded me the importance of staying sharp on the basics!

The basis of this question is returning the last word in a given string. If we look at Example 1, we can find a hint to get to our answer.

Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.

In the explanation, LeetCode mentions the length of the word with the last word in the string. This should lead us to believe that we will at some point be returning the length of the last word in our algorithm.

So s is a variable for a string of words and spaces. We are looking for the last word in the string, so in order to make this easier for us, we must get rid of the extra spacing.

Given the starter code:

var lengthOfLastWord = function(s) {
const words = s.trim()
}

If we think back to the good ol’ days of JavaScript basics, a few methods come to mind that we can utilize to get rid of spacing in an array. The first one being trim() which removes spaces at the beginning and end of a string. If we implement this method with example 1, it will turn the input “ Hello World ” into “Hello World”. Notice, it will remove the spacing within the array.

But what about the spacing in between “Hello World”? For this, we can use split(). The split method divides a string into an ordered list of substrings, puts them into an array, and returns the array. And since we’re writing in JavaScript, we can tag this onto our trim method!

var lengthOfLastWord = function(s) {
const words = s.trim().split(' ');
}

Lets take a second to focus on how we’re invoking the split method. Within the parenthesis we usually put a number for the index, but in this algorithm we are making only an array of words. By putting an empty space with quotes, we are getting rid of the space between “ Hello World” to “Hello,World”.

Now that we have a true array of words, we can identify the last word. What had me stumped was forgetting the pop() method! The pop method removes the last element from an array and returns that element. This changes the length of the array.

var lengthOfLastWord = function(s) {
const words = s.trim().split(' ');
const lastWord = words.pop
}

We’re not totally done yet! We have to account for the what if’s, like what if there isn’t a last element to return. To ensure for this possibility, we must use a logical operator to determine the logic between values. We will add an or operator so if there is no element in the array, then it will be an empty string. We must return zero if there is no element to return.

var lengthOfLastWord = function(s) {
const words = s.trim().split(' ');
const lastWord = words.pop || ' ';
return lastWord.length;
};

And finally, we return the length of the last word.

Conclusion:
What’s great about coding is that you are not limited to a specific answer, you can craft your own that makes sense to you. This particular answer made the most sense to me. When researching the logic behind this problem I came across a number of solutions that ranged in complication and length. But this solution is a great solution for coders like myself who are getting acquainted with the LeetCode grind and find comfort in the basics.

Resources:

--

--

Melissa Guachun

Software Developer and visual artist based in NYC. Join me on my journey to coding enlightenment or a torrential mental breakdown, whichever comes first.