easyStringArrays & Hashing

Length of Last Word

easyTime: O(n)Space: O(1)

Signals to notice

find last word in stringhandle trailing spacesstring traversal

Brute force first

Split by spaces, return length of last element — but creates extra strings. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

The key insight

Scan from the end, skip trailing spaces, count characters. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

What must stay true

Start from the end to find the last word without processing the entire string. As long as that statement keeps holding, you can trust the steps built on top of it.

Easy way to go wrong

Not handling trailing spaces — 'Hello World ' should return 5. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Arrays & Hashing Pattern