class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
arr = s.split(" ")
res = 0
for ch in arr:
if len(ch):
res = len(ch)
return res
class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
length = 0
res = 0
s += " "
for ch in s:
if ch==" ":
if length != 0:
res = length
length = 0
else:
length += 1
return res
class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
n = len(s)-1
res = 0
while(n>=0 and s[n]==" "): n -= 1
while(n>=0 and s[n]!=" "):
n -= 1
res += 1
return res