AC Python that deals with pathogenic cases such as " " or "a "


  • 0
    B
    def lengthOfLastWord(self, s):
        
        length=len(s)
        if length==0:
            return 0
        
        for i in range(length-1,-1,-1):
            if s[i]!=' ':
                break
        for j in range(i,-1,-1):
            if s[j]==' ':
                break
        
        if s[j]!=' ':
            return i-j+1
        else:
            return i-j

Log in to reply
 

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