Watch 10 video solutions for Length of Last Word, a easy level problem involving String. This walkthrough by NeetCode has 46,609 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
Example 1:
Input: s = "Hello World" Output: 5 Explanation: The last word is "World" with length 5.
Example 2:
Input: s = " fly me to the moon " Output: 4 Explanation: The last word is "moon" with length 4.
Example 3:
Input: s = "luffy is still joyboy" Output: 6 Explanation: The last word is "joyboy" with length 6.
Constraints:
1 <= s.length <= 104s consists of only English letters and spaces ' '.s.Problem Overview: You are given a string that may contain letters and spaces. The task is to return the length of the final word in the string. A word is defined as a sequence of non-space characters, and the input may include trailing spaces.
Approach 1: Split and Trim Approach (Time: O(n), Space: O(n))
This method relies on built-in string operations. First remove trailing spaces using trim() (or equivalent), then split the string by spaces using split(" "). The last element of the resulting array is the final word, so its length is the answer. The key idea is that trimming guarantees the last token is a valid word rather than an empty string.
This approach is straightforward and easy to read. Most languages implement split and trim efficiently, but splitting creates a new array of substrings which requires additional memory. Time complexity is O(n) because the string is scanned during trimming and splitting. Space complexity is also O(n) due to the array of words.
Approach 2: Reverse Traversal Approach (Time: O(n), Space: O(1))
This approach avoids extra memory by scanning the string from the end. Start from the last character and skip all trailing spaces. Once the first non-space character appears, begin counting characters until another space or the start of the string is reached. The counter represents the length of the last word.
The key insight is that the last word always appears immediately before the trailing spaces. By iterating backward, you only process the relevant characters without allocating additional data structures. The algorithm performs a single pass over the string, so time complexity is O(n), while space complexity remains O(1).
This problem is a classic exercise in string processing and pointer-style traversal. The reverse scan behaves similarly to a two pointers technique where one pointer moves from the end and counts characters of the last word.
Recommended for interviews: The reverse traversal approach is typically preferred. It demonstrates control over string iteration, avoids unnecessary allocations, and achieves O(1) extra space. The split-based method still shows good problem understanding and is acceptable for quick implementation, but interviewers often expect the in-place traversal because it is more memory efficient.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Split and Trim Approach | O(n) | O(n) | Quick implementation using built-in string functions; readability prioritized over memory efficiency |
| Reverse Traversal Approach | O(n) | O(1) | Optimal solution for interviews and memory-constrained scenarios |