MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / lengthOfLastWord

Method lengthOfLastWord

String/LengthOfLastWord.py:14–36  ·  view source on GitHub ↗

:type s: str :rtype: int

(self, s)

Source from the content-addressed store, hash-verified

12"""
13class Solution(object):
14 def lengthOfLastWord(self, s):
15 """
16 :type s: str
17 :rtype: int
18 """
19 if not s:
20 return 0
21
22 result = 0
23 flag = False
24
25 for i in s[::-1]:
26 if i == ' ' and not flag:
27 continue
28 elif i == ' ' and flag:
29 return result
30 else:
31 result += 1
32 flag = True
33 continue
34
35 return result
36 return result

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected