Palindromic Substrings - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

We count all palindromic substrings in the input by treating each position in the string as either the center of a palindrome (for odd-length palindromes) or the left side of a two-character center (for even-length palindromes), and expanding outward from that center.

⚠️ Important Note: While this method correctly counts all palindromic substrings, it does not achieve the optimal time complexity. A more advanced approach, such as Manacher’s Algorithm, can perform the same task more efficiently by using extra space.

  • Key idea:
    A palindrome reads the same forward and backward, so every palindrome is symmetric around some center. That center can be:
    • A single character — for odd-length palindromes like "aba", centered on the middle letter.
    • Between two characters — for even-length palindromes like "abba", centered between the two middle letters.
    If two characters mirror each other as we expand outward from the center, they form a palindrome. Here, every valid palindrome we encounter counts toward the total (not just the longest one).
  • Expanding around a center:
    We use a helper routine expandAroundCenter(left, right) to explore and count palindromes around a chosen center. For each center (for odd length we call it with left = i, right = i, for even length we call it with left = i, right = i + 1), it:
    • Checks characters at left and right.
    • If they match, that means s[left..right] is a palindrome, so we increment the global counter.
    • Then we expand outward by doing left-- and right++ to see if we can grow this palindrome further.
    • If they do not match (or we go out of bounds), expansion for this center stops — that center cannot produce any larger palindromes.
  • Odd-length and even-length palindromes:
    For each index i in the string:
    • Odd-length expansion: We call expandAroundCenter(i, i) and treat i as the center. This finds palindromes like "a", "aba", "racecar", etc.
    • Even-length expansion: We call expandAroundCenter(i, i + 1) and treat i and i + 1 as the two sides of the center. This finds palindromes like "aa", "abba", etc.
      Note that we always call this even if s[i] !== s[i + 1] — the helper immediately stops in that case, which is fine.
    We must do both calls for every i because palindromes can be either odd length or even length.
  • Counting palindromes:
    We maintain a running counter palindromeCount. Every time expandAroundCenter sees that s[left] === s[right], it means s[left..right] is a valid palindromic substring, so we immediately increment palindromeCount. We do not store substrings, we just count them.
  • Result:
    After we have expanded around every possible center in the string (both odd and even), palindromeCount holds the total number of palindromic substrings in the input, including duplicates and overlaps. That final count is returned.
  • Why this works:
    Every palindromic substring has a well-defined center, and expanding outward from that center will enumerate it exactly once. By checking every possible center and expanding until the symmetry breaks, we are guaranteed to count all palindromic substrings.

Time Complexity

Let n be the length of the input string s. For each index i, we expand outward twice — once for odd-length palindromes and once for even-length palindromes. Each expansion can scan outward up to O(n) characters in the worst case, and we do this for all n indices. Therefore, the total running time is O(n²).

Space Complexity

The algorithm uses just a few variables. It does not allocate any data structures or recursive calls that scale with input size. Therefore, the auxiliary space usage is O(1).

Visucodize editorial solution titled JS Solution for LeetCode's Palindromic Substrings 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/palindromic-substrings.