Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

Input: "Hello World"
Output: 5

Difficulty: introductory

Starter code:

class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        

Solutions:
class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         x = s.split()
         return len(x[-1]) if len(x) > 0 else 0

class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         '''
         if s == None: return 0
         count = 0
         for item in s[-1::-1]:
             if item == ' ':
                 break
             count += 1
         return count
         '''
         count = len(s.strip().split(' ')[-1])
         return count
                 
             
             


class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         return len(s.strip(' ').split(' ')[-1])

class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         ans,tail=0,len(s)-1
         while tail>=0 and s[tail]==' ':
             tail-=1
         while tail>=0 and s[tail]!=' ':
             ans+=1
             tail-=1
         return ans

class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         if not s:
             return 0
         
         s = s.rstrip()
         s = s.lstrip()
         
         for i in range(0,len(s))[::-1]:
             if s[i] == ' ':
                 return len(s)-1-i
         return len(s)
         
                 


class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         spaces = 0
         word_started = False
         # Traverse through string from back
         # Count number of spaces encountered before first word starts
         # Keep going through characters until next space is found
         # Return that index (minus one, not counting the space)
         # And subtract the number of spaces
         for i in range(1, len(s) + 1):
             if s[-i] == " " and word_started is False:
                 spaces += 1
             elif s[-i] == " " and word_started is True:
                 return i - 1 - spaces
             else:
                 word_started = True
         if word_started is True:
             return len(s) - spaces
         else:
             return 0

class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         words = s.split()
         if not words:
             return 0
         return len(words[-1])

class Solution:
     def lengthOfLastWord(self, s):
         res = 0;
         last = 0;
         for i in s:
             if(i == ' '):
                 res = 0;
             else:
                 res += 1;
                 last = res;
                 
         return last;
         """
         :type s: str
         :rtype: int
         """


class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         l = s.split(' ')
         while l:
             if l[-1]:
                 return len(l[-1])
             else:
                 l.pop(-1)
         return 0

class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         v = s.split()
         if not v:
             return 0
         else:
             return len(v[-1])


class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         l = len(s)
         n_c=0
         for i in range(l-1,-1,-1):#从末尾开始计数
             if s[i]!=' ':
                 n_c += 1
             elif s[i]==' ' and n_c == 0 :
                 pass
             elif s[i]==' ' and n_c != 0 : #遇到空格，且字符计数非零时，返回计数值
                 return n_c
         return n_c

class Solution:
     def lengthOfLastWord(self, s):
         """
         :type s: str
         :rtype: int
         """
         s = s.strip()
         if s == None: return 0
         count = 0
         for item in s[-1::-1]:
             if item == ' ':
                 break
             count += 1
         return count