Using Python and RegEx


  • 0
    L
    from re import search
    
    class Solution(object):
        def lengthOfLastWord(self, s):
            m = search('(\w+(?=\s+$))|(\w+$)', s)
            return len(m.group(0)) if m else 0
    

    Worked fine for me.


Log in to reply
 

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