here is my simple accepted solution, the idea is included in the comment
class Solution: # @param s, a string # @return an integer def lengthOfLastWord(self, s): # get rid of the " " before or after # split the string into words t=s.strip(" ").split(" ") # find the last word # return its length return len(t[len(t)-1])
You do not deal with the " " input.
And you can just use s.split() instead of s.strip(" ").split(" ").
the input of " " will return 0 as desired. As for s.split(), the input of "a b " will get a couple of "" at the end, while by using strip(" ") you can get rid of the unwanted ""s
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.