Length of Last Word
Length of Last Word is LeetCode 58 (Easy). Given a string s of English letters and spaces, return the length of the last word in it.
- A word is a run of non-space characters, with no space inside it.
smay start or end with spaces, and words may be separated by several spaces.- At least one word is guaranteed.
- 1 <= s.length <= 10⁴
- s consists of only English letters and spaces ' '.
- There will be at least one word in s.
Intuition
The last word sits at the end of the string, so read the string from the right. Two things can be there: spaces after the last word, and then the word itself.
- Skip: move left past every trailing space. You now stand on the last letter of the last word.
- Count: keep moving left and counting until you meet a space or pass the front of the string.
That count is the answer. Everything before that space is never read, so the scan touches only the last word and the spaces after it, and it needs no extra memory.
When a question only cares about the end of a string or array, start there and walk backwards. You stop as soon as you have the answer instead of tracking everything that came before it.
Approach
Before reading on, decide where to start reading and what to do with spaces at the very end. Then decide what tells you the last word has started. Aim for O(n) time and O(1) space.
Two ways to solve it
Skip trailing spaces from the right, then count letters to the next space.
- No copies: two indexes, nothing else.
- Early stop: earlier words are never read.
The usual interview answer.
Split on whitespace and measure the last piece.
- Short: one line in Python.
- Cost: builds a list of every word to use one.
Fine when you just need it to work.
Both are O(n) time. The reverse scan wins because it needs no extra memory and stops at the last word; the steps, code and live demo follow it, and the split version comes after the demo.
Start at the last index
Set i = len(s) - 1. Reading from the right reaches the last word first, so there is no need to remember any earlier word.
Skip the trailing spaces
While s[i] is a space, step left. Because the string holds at least one word, this loop always stops on a letter: the last letter of the last word.
Count the letters of the word
While i >= 0 and s[i] is a letter, add 1 to length and step left. The first space you meet, or the front of the string, marks where the last word begins, and length is the answer.
Code in Python, C++ and Java
i >= 0 check stops here before reading outside the string; the answer is 6.Split into words
Splitting on any run of whitespace drops empty pieces, so the last item is always the last word, even when the string ends in spaces. It reads the whole string and stores every word, which is why it uses O(n) space.
Common pitfalls
Splitting on a single space
return len(s.split(" ")[-1])return len(s.split()[-1])
split(" ") keeps an empty piece for every extra space, so a string that ends in a space ends with "". On "fly me " it returns 0 instead of 2.
Counting from the left and resetting on spaces
length = 0
for ch in s:
length = 0 if ch == " " else length + 1
return lengthlength = 0
for i, ch in enumerate(s):
if ch != " ":
length = length + 1 if i > 0 and s[i - 1] != " " else 1
return lengthThe trailing spaces reset the counter after the last word has been measured. On "Hello World " the left-to-right count ends at 0 instead of 5. Reset only when a new word starts.
Dropping the i >= 0 check while counting
while s[i] != " ":
while i >= 0 and s[i] != " ":
When the last word starts at index 0, as in "joyboy", i reaches -1. Java and C++ then read outside the string, and Python's s[-1] quietly wraps to the last letter and keeps counting until it crashes.
Complexity
s. In the worst case, one long word or a long run of trailing spaces, every character is read once. Only two integers are stored.Length of Last Word FAQ
Can the answer ever be 0?
Not here, because at least one word is guaranteed. If you reuse the code on a string that is all spaces, the skip loop runs past the front, the counting loop never starts, and it returns 0, which is the natural answer for "no words".