My Javascript solution


  • 0
    H

    Using a character dictionary:

    var longestPalindrome = function(s) {
        var dict = {};
        for(var i=0;i<s.length;i++){
            var ch = s.charAt(i);
            if(dict[ch]){
                dict[ch] = dict[ch]+1;
            }
            else dict[ch]=1;
        }
        var count=0;
        var hasOddValue=false;
        console.log(dict);
        for(var k in dict){
            if(dict[k]%2===0){
                count+=dict[k];
            }
            else {count+=dict[k]-1;}
            if(dict[k]%2!==0){hasOddValue=true;}
        }
        if(hasOddValue===true) return count+1;
        else return count;
    };
    

Log in to reply
 

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