02 · Arrays & Strings in Java

#58 Length of Last Word

Easysolve on LeetCode ↗

frequently reported atBloombergGoogleAmazonMicrosoft

the problem

In plain words

A string holds English letters and spaces, with at least one word in it. A word is a run of letters with no space inside. Return how many letters the last word has — spaces at the very end don't count.

in s = "Hello World"

out 5

The last word is "World".

in s = " fly me to the moon "

out 4

Trailing spaces are ignored; the last word is "moon".

in s = "luffy is still joyboy"

out 6

  • 1 ≤ s.length ≤ 10⁴
  • Only English letters and ' '
  • At least one word

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #125
      Valid Palindrome
      Easyin Two Pointers
    2. #71
      Simplify Path
      Mediumin Stacks