Length of Last Word
Problem: Length of Last Word
The solution is quite simple. Just find last word and decide whether it's a word. If not a word, we find the second last word and do the same repeating thing.
Code in Python:
class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
words = s.split(" ")
while words:
cur = words.pop()
if self.isWord(cur):
return len(cur)
return 0
def isWord(self, s):
if not len(s): return False
for c in s:
if c not in string.letters:
return False
return True