‹ DS&A interview · Socratic
DSA · Strings · #97

Length of Last Word

Module 62 · difficulty 2/5·30:00starts on first keystroke

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. Implement the function `lengthOfLastWord(s)`.

Examples
  • s = "Hello World" 5The last word is "World" with length 5.
  • s = " fly me to the moon " 4The last word is "moon" with length 4.
  • s = "luffy is still joyboy" 6The last word is "joyboy" with length 6.
Constraints
  • · 1 <= s.length <= 10^4
  • · s consists of only English letters and spaces ' '.
  • · There will be at least one word in s.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.