Problems / Length of Last Word
Easy

Length of Last Word

Easy 0 submissions 10 coins +50 XP
String
Problem Description
## Problem 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. ## Examples **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. Trailing spaces are ignored. ``` **Example 3:** ``` Input: s = "luffy is still joyboy" Output: 6 Explanation: The last word is "joyboy" with length 6. ``` ## Hints - Strip trailing spaces first, then find the last space to determine where the last word begins. - Alternatively, traverse from the end of the string, skip trailing spaces, then count non-space characters. - In Python, `s.split()[-1]` gives the last word directly, but try implementing it manually.
Constraints
- `1 <= s.length <= 10^4` - `s` consists of only English letters and spaces `' '`. - There will be at least one word in `s`.

Need help?

Connect with expert programmers for real-time collaborative coding, video meetings, and whiteboard sessions via CodeConnect.

Video Call Whiteboard Live Coding Screen Share