javascript - 1 line solution


  • 0
    N

    var countSegments = function(s) {
    return s.trim().length === 0 ? 0 : s.replace(/ (?= )/g,"").trim().split(" ").length;
    };

    After checking for no length, replace multiple spaces, trim, split then count length.


Log in to reply
 

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