Maximum Product of the Length of Two Palindromic Substrings - Problem
You are given a 0-indexed string s and are tasked with finding two non-intersecting palindromic substrings of odd length such that the product of their lengths is maximized.
More formally, you want to choose four integers i, j, k, l such that 0 <= i <= j < k <= l < s.length and both the substrings s[i...j] and s[k...l] are palindromes and have odd lengths. s[i...j] denotes a substring from index i to index j inclusive.
Return the maximum possible product of the lengths of the two non-intersecting palindromic substrings.
A palindrome is a string that is the same forward and backward. A substring is a contiguous sequence of characters in a string.
Input & Output
Example 1 — Basic Case
$
Input:
s = "ababbb"
›
Output:
9
💡 Note:
The optimal split is at position 3. Left part "aba" has palindrome "aba" (length 3), right part "bbb" has palindrome "bbb" (length 3). Product: 3 × 3 = 9.
Example 2 — Single Characters
$
Input:
s = "zaba"
›
Output:
1
💡 Note:
Split at position 1: left "z" (length 1), right "aba" has palindrome "aba" (length 3). But we can also split at position 2: left "za" has palindrome "a" (length 1), right "ba" has palindrome "a" (length 1). Best product is 1 × 3 = 3, but checking all positions gives us 1.
Example 3 — No Valid Split
$
Input:
s = "aa"
›
Output:
0
💡 Note:
We need odd-length palindromes. "aa" has even length, so there's no way to find two non-intersecting odd-length palindromes.
Constraints
- 2 ≤ s.length ≤ 105
- s consists of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Given string s = "ababbb"
2
Find Split
Try each position as boundary between left and right parts
3
Calculate Product
Find best palindromes in each part and multiply lengths
Key Takeaway
🎯 Key Insight: Use expand-around-centers to find palindromes efficiently, then precompute maximum lengths for quick split evaluation
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code