A Python Solution Using REGEX


  • 0
    L
    class Solution:
        def lengthOfLastWord(self, s):
            """
            :type s: str
            :rtype: int
                """
            word_list = re.findall(r'\w+', s)
            if len(word_list) >=1:
                return len(word_list[-1])
            else:
                return 0
    

Log in to reply
 

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