Length of Last Word
Return the length of the last word in a string, ignoring trailing spaces.
- 1 <= s.length <= 10⁴
- s consists of only English letters and spaces ' '.
- There will be at least one word in s.
Intuition
Length of last word returns the length of the final word in a string, where words are separated by spaces. The complication is entirely in the whitespace — the input may have trailing spaces, and a naive split handles them badly.
The cleanest reading works backwards from the end:
- Skip any trailing spaces first, then count characters until the next space or the start of the string.
Two loops, no splitting, no allocation. The first skips spaces from the end; the second counts the word it lands on.
The order is what makes it correct. Counting before skipping the trailing spaces returns 0 on any input ending in a space, which is exactly the case the problem is testing.
The library approach also works and is shorter — trim the string, split on whitespace, and take the last element's length. It allocates a list of every word to use only one of them, which is wasteful but perfectly acceptable at these input sizes.
Where the library version goes wrong is splitting without trimming first. Splitting "hello world " on a single space yields a trailing empty string, and taking the last element returns 0 rather than 5.
The problem guarantees at least one word exists, so there is no empty-input case to handle.
The backward scan uses O(1) space and stops as soon as the last word is measured, never examining the rest of the string — a genuine advantage when the string is long and the final word is short.
Walk backwards, skipping trailing spaces first, then count until the next space. Scanning from the end avoids tracking every word and stops as soon as the answer is known — the last word is the first one you meet.
Approach
Before reading on: price up what the direct approach costs here, then ask what you are recomputing on every character that could be carried instead. Aim for O(n) time and O(1) space.
Work backwards from the end
The last word is at the end, so scanning backwards finds it without examining the rest of the string. No splitting or allocation is required.
Skip trailing spaces first
Move the index left past any spaces before counting. Counting first returns 0 on any input ending in a space — precisely the case being tested.
Count until a space or the start
From the last non-space character, count leftwards until reaching a space or index 0. That count is the answer.
Trim before splitting in the library version
Splitting "hello world " on a space leaves a trailing empty string, so the last element has length 0. Trim first, then split.
Rely on the guarantee
The problem promises at least one word exists, so no empty-input case needs handling and the backward scan always terminates on a word.
Cost of the scan
Only the final word and its trailing spaces are examined, giving O(n) worst case but often far less, with O(1) space — better than splitting, which allocates every word.
Solution & live demo
Common pitfalls
Not skipping trailing spaces
i = len(s) - 1 while i >= 0 and s[i] != ' ':
while i >= 0 and s[i] == ' ':
i -= 1Inputs like "hello world " end in spaces, so the counting loop terminates immediately and returns 0. The trailing whitespace has to be consumed before counting begins.
Splitting the whole string
return len(s.split()[-1])
while i >= 0 and s[i] != ' ':
length += 1Correct, but it allocates a list of every word to use exactly one. The backward scan touches only the trailing whitespace and the final word.
Splitting on the literal space
parts = s.split(' ')
return len(parts[-1])# skip trailing spaces first
split(' ') preserves empty strings, so a trailing space makes the last element "" and the answer 0. Bare split() handles it, but the difference between the two is exactly the trap.
Edge cases
Skip past all trailing spaces first so the count starts on the real last character, not an empty token.
The backward walk runs to index 0 (or off the front), counting the whole string as one word.
Only the final run of non-space characters is counted; interior spacing is irrelevant.
Skipping trailing spaces walks the pointer past index 0, so the counting phase never starts and length is 0.