Step-by-Step Guide to Solving Leetcode 58: Length of Last Word

Saul Feliz
4 min readAug 8, 2020

--

Remember those edge cases!

While this problem might not be too difficult at first glance, I believe the main lesson of it is dealing with edge cases. The problem states that we’ll get a string of upper/lower case alphabet characters, along with white spaces. All we have to do is return a number that tells us how long the last word of that string is.

We don’t necessarily know how long this string will be. It could be one word. It could be a whole paragraph. It can even be empty. Thus, I think this is mainly an exercise of dealing with non-happy-path inputs. Let’s take a look.

Photo by Romain Vignes on Unsplash

Let’s imagine our input string (s) is “This is a Leetcode problem explanation”. The last word here is “explanation”. So our function should return 11. Something to immediately consider is punctuation. Notice our sentence doesn’t end in a period. That’s because the problem states we’ll only have to deal with spaces. However, if it didn’t specify that, we should assume we’d have to deal with all kinds of punctuations as well. Because we only have to deal with spaces, the first thing the function should do is deal with trailing spaces. Here’s how we start to do that:

function lengthOfLastWord(s) {
let newInput = s.trim();
}

We take our input s and use JavaScript’s trim method for strings to remove any leading and trailing whitespaces the input might have.

Next, we have to find the last word. The human way to do it would be to start from the end of the string, and look at every character until you find a space. To mimic that process, let’s first split our newly trimmed string into an array so that we can easily access the last element (i.e. word).

function lengthOfLastWord(s) {
let newInput = s.trim();
let sArr = newInput.split("");

}

We split the array using an empty string (“”) as our separator. sArr looks like the array below, and we can see the last word (explanation) in bold has 11 characters:

[
'T', 'h', 'i', 's', ' ', 'i', 's',
' ', 'a', ' ', 'L', 'e', 'e', 't',
'c', 'o', 'd', 'e', ' ', 'p', 'r',
'o', 'b', 'l', 'e', 'm', ' ', 'e',
'x', 'p', 'l', 'a', 'n', 'a', 't',
'i', 'o', 'n'

]

In order to find that length, let’s initialize a counter, and start counting from the end of our sArr object, until it reaches that first space:

function lengthOfLastWord(s) {
let newInput = s.trim();
let sArr = newInput.split("");
let counter = 0;
for (let i = newInput.length - 1; i > 0; i--) {
if (newInput[i] != " ") {
counter++;
} else {
return counter;
}
}

}

Normally, we begin for loops starting from the beginning and increment using i++. But this time, we start from the end, since it’s the last word we care about. We do that by initializing our iterator variable i to whatever the length of the array is, -1. We then count backwards from there, by decrementing i.

Next, we reach a conditional: if the character I’m currently seeing is not an empty space, increment the counter. As soon as I see an empty space, return the counter.

This makes sense because we’ve already trimmed spaces at the beginning and end of the string. Thus, the first time I see an empty space, it’s because we’ve completed traversing the last word. And at that point, we can stop, and return the counter variable, which has been increasing with every character seen.

The first time I tried this problem, I thought I was done. But I was wrong. This is only the happy-path solution.

Dealing with edge cases

Remember when I said we didn’t know how big this string would be? What if it’s empty? We should deal with that:

if (newInput.length === 0) return counter;

Also, what if the string is only one word? The way to test for that is if the input string doesn’t include a space. If that’s the case, we can return the length of the string, and we’re done. Here’s the full code:

function lengthOfLastWord(s) {
let newInput = s.trim();
let sArr = newInput.split("");
let counter = 0;
if (newInput.length === 0) return counter;if (!sArr.includes(" ")) return newInput.length;for (let i = newInput.length - 1; i > 0; i--) {
if (newInput[i] != " ") {
counter++;
} else {
return counter;
}
}
}

Questions? Let me know in some comments. And remember…

Grit > talent.

--

--