Skip to content

58. Length of Last Word

https://leetcode.com/problems/length-of-last-word/

js
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function(s) {
  var words = s.trim().split(/\s+/g)
  return words.length ? words[words.length - 1].length : 0
}
py
class Solution(object):

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