Easy understanding JavaScript solution


  • 0
    P
    var lengthOfLastWord = function(s) {
        var isSpace = false, count=0;
        for(let i=s.length-1; i>=0; i-=1){
            if(s.charAt(i)!==' '&&!isSpace){
                isSpace = true;
                count++;
            }else if(s.charAt(i)!==' '&&isSpace){
                count++;
            }else if(s.charAt(i)===' '&&isSpace){
                return count;
            }
        }
        return count;
    };
    

Log in to reply
 

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