JavaScript solution using regexp


  • 0
    1

    wish you to point out mistakes...

    var longestCommonPrefix = function(strs) {
        if(strs.indexOf('') > -1 || strs.length < 1){
            return '';   
        }
        
        strs.sort(function(a,b){return a.length - b.length;});
        var str = ',';
        var str0 = strs[0];
        var string = ',' + strs.toString();
        for(var i = 0,l = str0.length; i < l; i++){
            var reg = new RegExp(str + str0[i],"g");
            if(string.match(reg).length !== strs.length){
                break;
            }
            str += str0[i];
        }
        return str.replace(',','');
    };
    

  • 0
    1

    @18392185680 cost 95ms


Log in to reply
 

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