JS solution


  • 0
    var lengthOfLastWord = function(s) {
        let i = s.length - 1;
        while (s[i] === ' ') {
            i--;
        }
        // i is the index of the last non-empty character
        if (i === -1) return 0;
        if (i === 0) return 1;
        let j = i - 1;
        while (j >= 0 && s[j] !== ' ') {
            j--;
        }
        return i - j;
    };
    

Log in to reply
 

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