3 line Javascript solution


  • 0
    R
    var countSegments = function(s) {
        s = s.trim()
        if(s.length === 0) return 0;
        return s.replace(/\s+/g, ' ').split(' ').length;
    };

Log in to reply
 

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