SHARE
TWEET

Untitled

a guest Feb 14th, 2020 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @param {string} s
  3.  * @return {string}
  4.  */
  5. var longestPalindrome = function(s) {
  6.     if (s.length < 2) {
  7.         return s;
  8.     }
  9.    
  10.     let left = 0, right = 0, maxLeft = 0, maxRight = 0;
  11.    
  12.     for (let index = 0; index < s.length; index++) {
  13.         left = index, right = index;
  14.        
  15.         while (right < s.length - 1 && s[right] == s[right + 1]) {
  16.             right++;
  17.         }
  18.        
  19.         while (left > 0 && right < s.length - 1 && s[left - 1] === s[right + 1]) {
  20.             left--;
  21.             right++;
  22.         }
  23.        
  24.         if (maxRight - maxLeft == s.length - 1)
  25.             return s;
  26.         if (right - left > maxRight - maxLeft) {
  27.             maxLeft = left, maxRight = right;
  28.         }
  29.     }
  30.     return s.substring(maxLeft, maxRight + 1);
  31. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top