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:
3
💡 Note:
Split at position 1: left "z" has palindrome "z" (length 1), right "aba" has palindrome "aba" (length 3). Product: 1 × 3 = 3.
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code