AlgoMaster Logo

Length of Last Word

Last Updated: March 29, 2026

easy

Understanding the Problem

We need to find the length of the last word in a string. Sounds simple, but there's a catch: the string can have trailing spaces. "Hello World " has the last word "World" (length 5), not an empty string.

So the real task is: skip any trailing spaces, then count characters backward until you hit another space or the beginning of the string. That count is your answer.

Key Constraints:

  • 1 <= s.length <= 10^4 → With n up to 10,000, even O(n^2) would be fine. But this problem naturally has O(n) solutions, so there's no reason to overthink complexity.
  • s consists of only English letters and spaces → No special characters, tabs, or newlines to worry about. Simplifies parsing.
  • There is at least one word in s → We're guaranteed a valid answer exists. No need to handle the "no words" edge case.

Approach 1: Split and Find Last Word

Intuition

The most natural first thought: split the string into words and grab the last one. Most languages have a built-in split function that handles multiple spaces between words. After splitting, the last element of the resulting array is the last word, and its length is the answer.

There's a small nuance though. Some language's split functions may include empty strings in the result when there are trailing spaces, depending on the implementation. We need to be aware of this, but in most cases, splitting by spaces and picking the last non-empty word does the trick.

Algorithm

  1. Split the string s by spaces.
  2. Filter out any empty strings that may result from consecutive spaces.
  3. Take the last element of the resulting list.
  4. Return its length.

Example Walkthrough

Input:

0
1
2
3
f
4
l
5
y
6
7
m
8
e
9
10
11
12
t
13
o
14
15
16
17
t
18
h
19
e
20
21
m
22
o
23
o
24
n
25
26
s

After splitting: words = ["fly", "me", "to", "the", "moon"]

Last word is "moon", length = 4:

4
result

Code

This approach works correctly but processes the entire string and allocates extra memory.

What if we started from the end and just counted the characters of the last word directly?

Approach 2: Traverse from End (Optimal)

Intuition

Since we only care about the last word, why process the string from the beginning? Starting from the end, we can skip trailing spaces, then count non-space characters until we hit a space or the start of the string. That count is our answer.

This approach reads only as many characters as needed. For "Hello World", we only look at the last 5 characters plus however many trailing spaces there are. The earlier part of the string is never touched.

Algorithm

  1. Start from the last index of the string.
  2. Skip all trailing spaces by decrementing the index while the current character is a space.
  3. Count non-space characters by decrementing the index while the current character is not a space.
  4. Return the count.

Example Walkthrough

1Start from end: i=27
0
1
2
3
f
4
l
5
y
6
7
m
8
e
9
10
11
12
t
13
o
14
15
16
17
t
18
h
19
e
20
21
m
22
o
23
o
24
n
25
26
1/6

Code