TzookB

LeetCode - length-of-last-word challenge solution

Tue Jul 07 2020
Links:  question  |  code

This one was super easy at least after you understand the edge cases.

So I just trimmed the input string in case we get a string with whitespace in end.

After that, we split the sentence into words, take the last word and return the length of it.

def lengthOfLastWord(self, s: str) -> int:
    lastWord = s.strip().split(" ")[-1]
    return len(lastWord)