Watch 10 video solutions for Length of Last Word, a easy level problem involving String. This walkthrough by Technosage has 76,228 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 receive a string that may contain words separated by spaces, including trailing spaces at the end. The task is to return the length of the last word. A word is defined as a sequence of non-space characters. The challenge is mainly handling trailing spaces efficiently.
Approach 1: Split and Trim Approach (O(n) time, O(n) space)
The most straightforward strategy is to clean the string and split it into words. First remove leading and trailing spaces using trim() or equivalent. Then split the string using a space delimiter and access the final element of the resulting array. The length of that last element is the answer. This works because splitting converts the sentence into a list of individual words, making the last word easy to access.
This approach is easy to implement and readable in interviews, especially in languages with built-in string utilities. The tradeoff is extra memory usage because the split operation allocates an array of substrings. Time complexity is O(n) since the string is scanned to trim and split, and space complexity is O(n) due to storing all words. The solution mainly relies on operations from the string manipulation toolkit.
Approach 2: Reverse Traversal Approach (O(n) time, O(1) space)
A more efficient method scans the string from the end instead of creating intermediate structures. Start with a pointer at the last index and skip any trailing spaces by moving left while the character is a space. Once the first non-space character is found, begin counting characters until another space or the start of the string appears. The counter value represents the length of the last word.
This technique avoids splitting or allocating extra arrays. It performs a single linear scan and maintains only a few variables. Time complexity remains O(n), but space complexity drops to O(1) because no additional data structures are created. Conceptually, it behaves like a backward pointer scan similar to patterns used in two pointers problems, while still focusing on string traversal.
Recommended for interviews: Interviewers typically expect the reverse traversal approach. The split-based method demonstrates basic string manipulation and quickly produces a working solution, but it uses extra memory and hides the underlying logic. The reverse scan shows that you understand how to iterate through a string carefully, handle trailing spaces, and compute the answer in constant space. Starting with the simple approach and then optimizing to the reverse traversal method demonstrates both correctness and algorithmic awareness.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Split and Trim Approach | O(n) | O(n) | When quick implementation and readability matter more than memory usage |
| Reverse Traversal Approach | O(n) | O(1) | Preferred in interviews and memory-constrained scenarios |