solution1
class Solution:
# time O(N) space O(N)
def lengthOfLastWord(self, s: str) -> int:
l = 0
r = len(s)-1
# get the right space
while r>=0 and s[r] == ' ':
r -= 1
# count the last word (devided by space) by increamenting l
while r>= 0 and s[r] != ' ':
l += 1
r -= 1
return l
«««< HEAD «««< HEAD ======= =======
f11e408483b696119d803ad8c6b1f8ec5efa0e1a solution2
class Solution: def lengthOfLastWord(self, s: str) -> int: # case1 : check if empty strip = s.strip() if strip == '': return 0 # case2: check "a " will return several "" entry in the list l = s.split(' ') while len(l[-1])==0: l.pop() return len(l[-1]) <<<<<<< HEADtest 1st ======= ``` f11e408483b696119d803ad8c6b1f8ec5efa0e1a