Skip to content

Commit 1d9cc03

Browse files
authored
Merge pull request #3898 from revanth1718/main
Add Solution to LC problem 1960 (Hard)
2 parents fa0a7e5 + 0bab6e4 commit 1d9cc03

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
---
2+
id: maximum-product-of-length-of-two-palindromic-substrings
3+
title: Maximum Product of the Length of Two Palindromic Substrings
4+
sidebar_label: Maximum Product of the Length of Two Palindromic Substrings
5+
tags: [String, Palindrome, C++, Python, Java]
6+
description: Solve the problem of finding the maximum product of the lengths of two non-intersecting palindromic substrings in a given string.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
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.
14+
15+
More formally, you want to choose four integers `i, j, k, l` such that $0 \leq i \leq j < k \leq l \lt 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.
16+
17+
Return the maximum possible product of the lengths of the two non-intersecting palindromic substrings.
18+
19+
A palindrome is a string that is the same forward and backward. A substring is a contiguous sequence of characters in a string.
20+
21+
### Example
22+
23+
**Example 1:**
24+
```
25+
Input: s = "ababbb"
26+
Output: 9
27+
```
28+
**Explanation:** Substrings "aba" and "bbb" are palindromes with odd length. product = 3 * 3 = 9.
29+
30+
31+
### Constraints
32+
33+
- $2 \leq s.length \leq 10^5$
34+
- `s` consists of lowercase English letters.
35+
36+
## Solution
37+
38+
### Intuition
39+
40+
To solve this problem, we can use dynamic programming and a sliding window approach to find all possible palindromic substrings of odd length. By iterating through the string, we can determine the longest palindromic substring ending at each position and the longest palindromic substring starting at each position. Then, we calculate the maximum product of the lengths of two non-intersecting palindromic substrings.
41+
42+
### Time Complexity and Space Complexity Analysis
43+
44+
- **Time Complexity**: The solution involves a linear scan of the string and dynamic programming updates, making the time complexity $O(n)$.
45+
- **Space Complexity**: The space complexity is $O(n)$ due to the storage required for dynamic programming arrays.
46+
47+
### Code
48+
49+
#### C++
50+
51+
```java
52+
class Solution {
53+
public int maxProduct(String s) {
54+
int n = s.length();
55+
int[] left = new int[n];
56+
int[] right = new int[n];
57+
58+
// Calculate longest palindromic substring length ending at each index
59+
int l = 0, r = -1;
60+
for (int i = 0; i < n; i++) {
61+
if (i > r) {
62+
l = r = i;
63+
while (l >= 0 && r < n && s.charAt(l) == s.charAt(r)) {
64+
l--;
65+
r++;
66+
}
67+
left[i] = r - l - 1;
68+
} else {
69+
int k = (r - i) / 2;
70+
if (left[i - k] / 2 + i < r) {
71+
left[i] = left[i - k];
72+
} else {
73+
l = i - (r - i);
74+
while (l >= 0 && r < n && s.charAt(l) == s.charAt(r)) {
75+
l--;
76+
r++;
77+
}
78+
left[i] = r - l - 1;
79+
}
80+
}
81+
}
82+
83+
// Calculate longest palindromic substring length starting at each index
84+
l = n - 1;
85+
r = n;
86+
for (int i = n - 1; i >= 0; i--) {
87+
if (i < l) {
88+
l = r = i;
89+
while (l >= 0 && r < n && s.charAt(l) == s.charAt(r)) {
90+
l--;
91+
r++;
92+
}
93+
right[i] = r - l - 1;
94+
} else {
95+
int k = (i - l) / 2;
96+
if (right[i + k] / 2 + i > l) {
97+
right[i] = right[i + k];
98+
} else {
99+
l = i + (i - l);
100+
while (l >= 0 && r < n && s.charAt(l) == s.charAt(r)) {
101+
l--;
102+
r++;
103+
}
104+
right[i] = r - l - 1;
105+
}
106+
}
107+
}
108+
109+
// Calculate the maximum product
110+
```
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
int maxProduct(string s) {
116+
int n = s.size();
117+
vector<int> left(n, 0), right(n, 0);
118+
119+
// Calculate longest palindromic substring length ending at each index
120+
for (int i = 0, l = 0, r = -1; i < n; i++) {
121+
if (i > r) {
122+
l = r = i;
123+
while (l >= 0 && r < n && s[l] == s[r]) l--, r++;
124+
left[i] = r - l - 1;
125+
} else {
126+
int k = (r - i) / 2;
127+
if (left[i - k] / 2 + i < r) {
128+
left[i] = left[i - k];
129+
} else {
130+
l = i - (r - i);
131+
while (l >= 0 && r < n && s[l] == s[r]) l--, r++;
132+
left[i] = r - l - 1;
133+
}
134+
}
135+
}
136+
137+
// Calculate longest palindromic substring length starting at each index
138+
for (int i = n - 1, l = n - 1, r = n; i >= 0; i--) {
139+
if (i < l) {
140+
l = r = i;
141+
while (l >= 0 && r < n && s[l] == s[r]) l--, r++;
142+
right[i] = r - l - 1;
143+
} else {
144+
int k = (i - l) / 2;
145+
if (right[i + k] / 2 + i > l) {
146+
right[i] = right[i + k];
147+
} else {
148+
l = i + (i - l);
149+
while (l >= 0 && r < n && s[l] == s[r]) l--, r++;
150+
right[i] = r - l - 1;
151+
}
152+
}
153+
}
154+
155+
// Calculate the maximum product
156+
int maxProduct = 0;
157+
for (int i = 0; i < n - 1; i++) {
158+
maxProduct = max(maxProduct, (left[i] / 2) * (right[i + 1] / 2));
159+
}
160+
return maxProduct;
161+
}
162+
};
163+
```
164+
#### Python
165+
```python
166+
class Solution:
167+
def maxProduct(self, s: str) -> int:
168+
n = len(s)
169+
left = [0] * n
170+
right = [0] * n
171+
172+
# Calculate longest palindromic substring length ending at each index
173+
l, r = 0, -1
174+
for i in range(n):
175+
if i > r:
176+
l, r = i, i
177+
while l >= 0 and r < n and s[l] == s[r]:
178+
l -= 1
179+
r += 1
180+
left[i] = r - l - 1
181+
else:
182+
k = (r - i) // 2
183+
if left[i - k] // 2 + i < r:
184+
left[i] = left[i - k]
185+
else:
186+
l = i - (r - i)
187+
while l >= 0 and r < n and s[l] == s[r]:
188+
l -= 1
189+
r += 1
190+
left[i] = r - l - 1
191+
192+
# Calculate longest palindromic substring length starting at each index
193+
l, r = n - 1, n
194+
for i in range(n - 1, -1, -1):
195+
if i < l:
196+
l, r = i, i
197+
while l >= 0 and r < n and s[l] == s[r]:
198+
l -= 1
199+
r += 1
200+
right[i] = r - l - 1
201+
else:
202+
k = (i - l) // 2
203+
if right[i + k] // 2 + i > l:
204+
right[i] = right[i + k]
205+
else:
206+
l = i + (i - l)
207+
while l >= 0 and r < n and s[l] == s[r]:
208+
l -= 1
209+
r += 1
210+
right[i] = r - l - 1
211+
212+
# Calculate the maximum product
213+
maxProduct = 0
214+
for i in range(n - 1):
215+
maxProduct = max(maxProduct, (left[i] // 2) * (right[i + 1] // 2))
216+
return maxProduct
217+
```

0 commit comments

Comments
 (0)