LeetCode 1960. Maximum Product of the Length of Two Palindromic Substrings Solution in Java, C++, Python & Go | Explanation + Code

CoderIndeed
0
1960. Maximum Product of the Length of Two Palindromic Substrings

Description

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.

 

Example 1:

Input: s = "ababbb"
Output: 9
Explanation: Substrings "aba" and "bbb" are palindromes with odd length. product = 3 * 3 = 9.

Example 2:

Input: s = "zaaaxbbby"
Output: 9
Explanation: Substrings "aaa" and "bbb" are palindromes with odd length. product = 3 * 3 = 9.

 

Constraints:

  • 2 <= s.length <= 105
  • s consists of lowercase English letters.

Solutions

Solution 1

PythonJavaC++Go
class Solution: def maxProduct(self, s: str) -> int: n = len(s) hlen = [0] * n center = right = 0 for i in range(n): if i < right: hlen[i] = min(right - i, hlen[2 * center - i]) while ( 0 <= i - 1 - hlen[i] and i + 1 + hlen[i] < len(s) and s[i - 1 - hlen[i]] == s[i + 1 + hlen[i]] ): hlen[i] += 1 if right < i + hlen[i]: center, right = i, i + hlen[i] prefix = [0] * n suffix = [0] * n for i in range(n): prefix[i + hlen[i]] = max(prefix[i + hlen[i]], 2 * hlen[i] + 1) suffix[i - hlen[i]] = max(suffix[i - hlen[i]], 2 * hlen[i] + 1) for i in range(1, n): prefix[~i] = max(prefix[~i], prefix[~i + 1] - 2) suffix[i] = max(suffix[i], suffix[i - 1] - 2) for i in range(1, n): prefix[i] = max(prefix[i - 1], prefix[i]) suffix[~i] = max(suffix[~i], suffix[~i + 1]) return max(prefix[i - 1] * suffix[i] for i in range(1, n))(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !