[LeetCode] Top Interview 150 Problems Solving # 58 Length of Last Word
Understanding the Problem
A string is given. It has spaces in between. The task is to return the last string’s length. If the string is s = “Hello world”, the last string will be ”world” and its length will be 5, so return 5.
Approach
My approach will be to split(), get the last index and return length of it.
Solution
It was a simple problem. It would take a few lines without any help of the internal functions but it took one line with it.
First, split the string by space, then get the last index with [-1], then get the length of the last index with len().
class Solution:
def lengthOfLastWord(self, s: str) -> int:
return len(s.split()[-1])
Reflection
Nothing much to say about this. I was happy to see that this kind of a problem could show in interviews.
About the author
South Korean, master's degree of AI. A research focused AI specialist, and a rogrammer for LLM. I am seeking opportunities worldwide. I used to live in Frankfurt, Moscow, Pretoria, Baguio. Where to next?
Comments
Join the discussionNo comments yet. Be the first to comment.




