#5: Longest Palindromic Substring (Medium)

Given a string s, return the longest palindromic substring in s. A string is called a palindrome string if the reverse of that string is the same as the original string.

Example 1:

Input: s = “babad” Output: “bab” Explanation: “aba” is also a valid answer.

Example 2:

Input: s = “cbbd” Output: “bb”

Constraints:

1 <= s.length <= 1000 s consist of only digits and English letters.

Solution:

/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function (string) {
   let longestPalLength = 0;
   let longestPalLeft = 0;
   let longestPalRight = 0;

   var getLongestPalindrome = function (leftPosition, rightPosition) {
       // While there is space to expand, and the expanded strings match
       while (
           leftPosition >= 0 &&
           rightPosition < string.length &&
           string[leftPosition] === string[rightPosition]
       ) {
           //expand in each direction.
           leftPosition--;
           rightPosition++;
       }

       // Store the longest palindrome (if it's the longest one found so far)
       if (rightPosition - leftPosition > longestPalLength) {
           longestPalLeft = leftPosition + 1;
           longestPalRight = rightPosition - 1;
           longestPalLength = longestPalRight - longestPalLeft + 1;
       }
   };

   // Loop through the letters
   for (let i = 0; i < string.length; i++) {
       // Find the longest odd palendrome
       getLongestPalindrome(i, i + 1);

       // Find the longest even palendrome
       getLongestPalindrome(i, i);

       // Check if a longer palindrome cannot be found
       if ((string.length - i) * 2 < longestPalLength) {
           // Break out to avoid unnecessary computation
           break;
       }
   }

   return string.slice(longestPalLeft, longestPalRight + 1);
};

Explanation:

  None

See It On Github

Try It On LeetCode