29 ms Python solution.


  • 0
    Z
    class Solution(object):
        def lengthOfLastWord(self, s):
            cnt = 0
            for i in s[::-1]:
                if i != " ": 
                    cnt+=1
                if i == " " and cnt !=0: 
                    break
            return cnt
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.