blob: c467206715fdee90d05331555de57ffaab4f0abb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# 58. Length of Last Word
"""
given a string 's' consisting of words and spaces, return the length of the
last word in the string. a word is a maximal substring consisting of non
space characters only.
"""
class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
end = len(s) - 1
while end > 0 and s[end] == " ":
end -= 1
begin = end
while begin >= 0 and s[begin] != " ":
begin -= 1
return end - begin
if __name__ == "__main__":
obj = Solution()
print(obj.lengthOfLastWord("Hello World"))
print(obj.lengthOfLastWord(" fly me to the moon "))
print(obj.lengthOfLastWord("luffy is still joyboy"))
|