Python solution


  • 0
    P
    class Solution(object):
        def lengthOfLastWord(self, s):
            """
            :type s: str
            :rtype: int
            """
            if len(s) == 0:
                return 0
            count = 0
            empty = True
            for i in range(len(s)-1, -1, -1):
                if s[i] == ' ' and empty == True:
                    pass
                elif s[i] != ' ':
                    count += 1
                    empty = False
                else:
                    return count
            return count
            ```

Log in to reply
 

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