Javascript solution Beats 99%


  • 0
    S
    var longestPalindrome = function(s) {
      if (s === null || s.length === 0) return 0;
      var map = Array(128).fill(0);
      var count = 0;
      var flag = false;
      s.split("").forEach(function(c) {
        map[c.charCodeAt(0)]++;
      });
    
      for (var i = 0; i < 128; i++) {
        if (map[i] % 2 === 0) {
          count += map[i];
        } else {
          count += map[i] - 1;
          flag =  true;
        }
      }
    
      return flag ? count + 1 : count;
    };
    

Log in to reply
 

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