Maximum Product of the Length of Two Palindromic Substrings - Problem

Given a string s, you need to find two non-overlapping palindromic substrings of odd length such that the product of their lengths is maximized.

A palindrome reads the same forwards and backwards (like "aba", "racecar"). The substrings must be non-intersecting, meaning the first palindrome must end before the second one begins.

Goal: Find indices i, j, k, l where 0 ≤ i ≤ j < k ≤ l < s.length such that:

  • Both s[i...j] and s[k...l] are palindromes
  • Both have odd lengths
  • Their length product (j-i+1) × (l-k+1) is maximized

Example: In "ababbb", we could choose palindromes "aba" (length 3) and "bbb" (length 3) for a product of 9.

Input & Output

example_1.py — Basic case with clear palindromes
$ Input: s = "ababbb"
Output: 9
💡 Note: We can choose palindromes "aba" (indices 0-2, length 3) and "bbb" (indices 3-5, length 3). The product is 3 × 3 = 9.
example_2.py — Overlapping vs non-overlapping choice
$ Input: s = "zaaaxbbby"
Output: 9
💡 Note: We can choose "aaa" (indices 1-3, length 3) and "bbb" (indices 5-7, length 3). Even though "zaaaxbbby" contains longer individual palindromes, we need non-overlapping ones.
example_3.py — Single character palindromes
$ Input: s = "abcdef"
Output: 1
💡 Note: No multi-character palindromes exist, so we choose two single characters (each of length 1). The product is 1 × 1 = 1.

Visualization

Tap to expand
a b a | b b bSplit PointPalindrome 1Palindrome 2"aba" (length 3)"bbb" (length 3)Maximum Product: 3 × 3 = 9
Understanding the Visualization
1
Identify All Palindromes
Use efficient algorithms to find all palindromic substrings of odd length
2
Try Split Points
For each position, consider it as a boundary between the two palindromes
3
Find Best Left Palindrome
In the left partition, find the longest odd-length palindrome
4
Find Best Right Palindrome
In the right partition, find the longest odd-length palindrome
5
Calculate Product
Multiply the lengths and track the maximum product across all splits
Key Takeaway
🎯 Key Insight: The problem becomes manageable when we realize we can try every possible split point and use efficient palindrome detection algorithms to find the best palindromes on each side of the split.

Time & Space Complexity

Time Complexity
⏱️
O(n^5)

O(n^2) split points × O(n^2) substring pairs × O(n) palindrome verification

n
2n
Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
Linear Space

Constraints

  • 2 ≤ s.length ≤ 105
  • s consists only of lowercase English letters
  • Both palindromic substrings must have odd length
  • The substrings must be non-intersecting (first ends before second begins)
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 24
52.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen