title | author | type | date | url | desc | source |
---|---|---|---|---|---|---|
LeetCode - length-of-last-word challenge solution |
tzookb |
post |
2020-07-07 21:07:08 +0000 |
/leetcode/length-of-last-word |
leetcode length-of-last-word exercise |
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)