Share my concise JavaScript solution


  • 2
    L
    var longestPalindrome = function(s) {
      var max = '';
      for (var i = 0; i < s.length; i++) {
        for (var j = 0; j < 2; j++) {
          var left = i;
          var right = i + j;
          while (s[left] && s[left] === s[right]) {
            left--;
            right++;
          }
          if ((right - left - 1) > max.length) {
            max = s.substring(left + 1, right);
          }
        }
      }
      return max;
    };

Log in to reply
 

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