Leetcode Problem 58. Length of Last Word

58. Length of Last Word

Leetcode Solutions

Approach: String Index Manipulation

  1. Initialize a variable length to 0 to keep track of the length of the last word.
  2. Start iterating from the end of the string towards the beginning.
  3. Skip any trailing spaces at the end of the string.
  4. Once a non-space character is encountered, increment length for each subsequent non-space character.
  5. If a space is encountered after counting the non-space characters, break the loop as the last word has been fully traversed.
  6. Return the value of length.
UML Thumbnail

Approach: Built-in String Functions

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...