JavaScript Solution, 3 lines


  • 0
    var lengthOfLastWord = function(s) {
        s = s.trim();
        var arr = s.split(' ');
        return arr[arr.length - 1].length;
    };

  • 0

    This worked for me.

    var lengthOfLastWord = function(s) {
        return s.trim().split(" ").pop().length;
    };
    

Log in to reply
 

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