Longest Common Prefix JavaScript Solution not getting accepted


  • 0
    N

    I have a javascript solution which is working in browser console, but Leetcode is not accepting it. What's the problem?
    Can anyone please let me know.

    TIA

    var longestCommonPrefix = function(strs) {
        var str = strs.sort();
        var last=str[str.length-1],first = str[0],i=0;
        var result='';
        for(i=0;i<first.length;i++){
            if(first[i]===last[i]){
                result+=first[i];
            }
        }
        return result;
    };
    

Log in to reply
 

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