Longest Palindromic Substring - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

We find the longest palindromic substring by treating every position in the string as either the center of a palindrome (for odd-length cases) or the left side of a two-character center (for even-length cases), and expanding outward from the center.
⚠️ Important Note: While this algorithm correctly finds the longest palindromic substring, it does not have the optimal time complexity. A more efficient solution called Manacher’s Algorithm achieves better performance by using additional space.

  • Key idea:
    A palindrome reads the same forward and backward, which means it is symmetric around a center. That center can be:
    • One character — odd-length palindromes like "aba", centered at the middle letter.
    • Between two characters — even-length palindromes like "abba", centered between two identical letters.
    By expanding outward from the center while characters match, we can discover every palindrome in the string.
  • Expanding around center:
    This step is handled by the expandAroundCenter helper, which explores the largest possible palindrome around a given center. For each center (for odd length called with left = i, right = i and for even length called with left = i, right = i + 1), it:
    • Assumes that the given left and right pointers mark the exclusive boundaries of the current palindrome at the start of expansion.
    • While both pointers are within bounds and s[left] === s[right], the substring s[left..right] is accepted as a valid palindrome by moving left and right outward.
    • After expansion ends, since left and right represent the excluded boundaries, the helper returns the inclusive palindrome boundaries as [left + 1, right - 1].
  • Odd vs even palindromes:
    For each index i:
    • Odd-length check: Call expandAroundCenter(i, i), treating i as the center to find palindromes like "racecar".
    • Even-length check: Call expandAroundCenter(i, i + 1), treating i as the first character of a two-character center to find palindromes like "noon". The helper treats these initial indices as exclusive boundaries, so there’s no need to check whether s[i] and s[i + 1] match before making the call.
    Both checks are necessary because the longest palindrome could be either odd or even in length.
  • Tracking the best palindrome:
    For each expansion, we maintain the variables below:
    • largestPalindromeStartIndex → starting index of the current longest palindrome
    • largestPalindromeIndexDiff → the difference between end and start indices of the current longest palindrome
    If the current palindrome’s index difference (end - start) is larger than the recorded largestPalindromeIndexDiff, it means we’ve found a new longest palindrome so far, so both variables are updated accordingly. (Since the palindrome’s actual length is end - start + 1, a larger index difference always implies a longer palindrome.)
  • Result:
    After testing all centers, the final answer is s.substring(largestPalindromeStartIndex, largestPalindromeStartIndex + largestPalindromeIndexDiff + 1), representing the longest palindromic substring in the input string.
  • Why this works:
    Every palindrome must have a center. By expanding outward from every possible center and keeping the longest match, we are guaranteed to find the longest palindromic substring without redundant work.
  • Complexity:
    There are O(n) centers (two per character: odd and even), and each expansion may take up to O(n). Hence, the total time complexity is O(n²), and the algorithm uses only O(1) extra space.

Time Complexity

Let n be the length of the input string s. For each index i, we try to expand a palindrome in two ways: treating i as the center (odd-length case) and treating i and i + 1 as the center (even-length case). Each such expansion can grow outward up to the full length of the string in the worst case.

There are O(n) possible centers, and each expansion can take up to O(n) in the worst case, so the total running time is O(n²).

Space Complexity

The algorithm uses a constant amount of extra state: a few indices (left, right, largestPalindromeStartIndex, largestPalindromeIndexDiff) and temporary values returned from expansion. It does not allocate any data structures or build a recursion tree that scales with input size. Therefore, the auxiliary space usage is O(1).

Visucodize editorial solution titled JS Solution for LeetCode's Longest Palindromic Substring coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/longest-palindromic-substring.