Palindromic Substrings

Palindromic Substrings

Given a string `s`, find and return the total number of sub strings in `s` that are palindromes.

A palindrome is a string that remains the same when read both forward and backward.

palindromic substrings problem

Constraints:

  • 1 <= s.length <= 1000
  • “s” consists of lowercase English letters.

Palindromic Substrings Solution

Approaches for solving Palindromic substrings problem

There are mainly 5 approach to solve this problem 

  1. Brute Force Method
  2. Dynamic Programming Method
  3. Two Pointers Method
  4. Two Pointers (Optimal) Method
  5. Manacher’s algorithms

Approach 1 : Brute Force

  • Check every possible substring in the string.
  • For each substring, see if it reads the same forward and backward.
  • This method is simple but slow because it checks all combinations.

Approach 3 : Two Pointers

  • Think of each character (and pair of characters) as the center of a palindrome.
  • Expand outward, checking if characters match, and count all palindromes found.
  • This method is faster and uses less memory.

Approach 5 : Manacher’s Problem

  • Preprocess the string with separators to unify palindrome checks, and maintain an array that tracks the radius of the longest palindrome at each position.
  • Use symmetry to skip redundant checks and expand efficiently.

 

 

Approach 2 : Dynamic Programming

  • Use a table to store if a sub string is a palindrome.
  • Start with small sub strings (length 1 and 2), then use this information to check larger ones.
  • This saves time by reusing results from smaller sub strings.

Approach 4 : Two Pointers (Optimal)

  • Use two pointers to expand around potential centers of palindromes.
  • Treat each character and pair of consecutive characters as centers (to handle odd and even-length palindromes).
  • Expand outward from each center and count valid palindromic substrings.

1. Brute Force Method:

Check all possible substrings in the string and verify each one to see if it is a palindrome. This method is simple but slow because it explores all combinations.

  • Time complexity: O(n^3)
  • Space complexity: O(1)

Code

2. Dynamic Programming  Method

Use a table to track if sub strings are palindromes, starting with smaller ones and building up to larger ones. This approach reuses previous results to avoid redundant checks.

  • Time complexity: O(n^2)
  • Space complexity: O(n^2)

Code

3. Two Pointers Method

Expand around each character (or pair of characters) as the center of a potential palindrome. Count all valid palindromes found by comparing characters outward.

  • Time complexity: O(n^2)
  • Space complexity: O(1)

Code

4. Two Pointers (Optimal) Method

Similar to the regular two-pointer method but optimized to handle odd and even-length palindromes efficiently. It avoids unnecessary checks and uses O(1) space.

  • Time complexity: O(n^2)
  • Space complexity: O(1)

Code

5. Manacher’s Algorithm

This method transforms the string to handle odd and even-length palindromes uniformly by adding separators (e.g., #) between characters. It then uses a center-expansion technique with precomputed symmetry to find all palindromic substrings in O(n) time.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

More Articles