Leetcode/python/58_length_of_last_word.py

10 lines
243 B
Python
Raw Normal View History

2024-04-08 15:29:49 +00:00
class Solution:
def lengthOfLastWord(self, s: str) -> int:
end = len(s)-1
while end >= 0 and s[end] == " ":
end -= 1
i = end
while i >= 0 and s[i] != " ":
i -= 1
return end-i