My simple Javascript solution


  • 0
    K
    var longestCommonPrefix = function(strs) {
    if(!strs || !strs[0])return "";
    if(strs.length==1)return strs[0];
    var first=strs[0];
    var r="";
    for(var i=0;i<first.length;i++)
    {
        for(var j=1;j<strs.length;j++){
            if(!strs[j])return "";
            var c=strs[j].charAt(i);
            if(c!=first[i])return r;
        }
        r=r+c;
    }
    return r;
    

    };


Log in to reply
 

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