Skip to content

Practice Length of Last Word

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#58
LeetCode Problem

Length of Last Word

Problem Statement

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.

Source: Length of Last Word on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "Hello World"
Output
5

# Example 2

Input
s = "   fly me   to   the moon  "
Output
4

# Example 3

Input
s = "luffy is still joyboy"
Output
6

Constraints

1 <= s.length <= 10^4
s consists of only English letters and spaces ' '.
There will be at least one word in s.

Related LeetCode Problems

Practice more problems similar to Length of Last Word.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.