1960. Maximum Product of the Length of Two Palindromic Substrings LeetCode Solution

In this guide, you will get 1960. Maximum Product of the Length of Two Palindromic Substrings LeetCode Solution with the best time and space complexity. The solution to Maximum Product of the Length of Two Palindromic Substrings problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Product of the Length of Two Palindromic Substrings solution in C++
  4. Maximum Product of the Length of Two Palindromic Substrings solution in Java
  5. Maximum Product of the Length of Two Palindromic Substrings solution in Python
  6. Additional Resources
1960. Maximum Product of the Length of Two Palindromic Substrings LeetCode Solution image

Problem Statement of Maximum Product of the Length of Two Palindromic Substrings

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.

See also  3090. Maximum Length Substring With Two Occurrences LeetCode Solution

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1960. Maximum Product of the Length of Two Palindromic Substrings LeetCode Solution in C++

class Solution {
 public:
  long long maxProduct(string s) {
    const int n = s.length();
    long ans = 1;
    // maxLeft[i] := the maximum odd length of palindromes in s[0..i]
    vector<int> maxLeft = manacher(s, n);
    // maxRight[i] := the maximum odd length of palindromes in s[i..n - 1]
    vector<int> maxRight = manacher(string{s.rbegin(), s.rend()}, n);

    ranges::reverse(maxRight);

    for (int i = 1; i < n; ++i)
      ans = max(ans, static_cast<long>(maxLeft[i - 1]) * maxRight[i]);

    return ans;
  }

 private:
  vector<int> manacher(const string& s, int n) {
    vector<int> maxExtends(n);
    vector<int> leftToRight(n, 1);
    int center = 0;

    for (int i = 0; i < n; ++i) {
      const int r = center + maxExtends[center] - 1;
      const int mirrorIndex = center - (i - center);
      int extend = i > r ? 1 : min(maxExtends[mirrorIndex], r - i + 1);
      while (i - extend >= 0 && i + extend < n &&
             s[i - extend] == s[i + extend]) {
        leftToRight[i + extend] = 2 * extend + 1;
        ++extend;
      }
      maxExtends[i] = extend;
      if (i + maxExtends[i] >= r)
        center = i;
    }

    for (int i = 1; i < n; ++i)
      leftToRight[i] = max(leftToRight[i], leftToRight[i - 1]);

    return leftToRight;
  }
};
/* code provided by PROGIEZ */

1960. Maximum Product of the Length of Two Palindromic Substrings LeetCode Solution in Java

class Solution {
  public long maxProduct(String s) {
    final int n = s.length();
    long ans = 1;
    // maxLeft[i] := the maximum odd length of palindromes in s[0..i]
    int[] maxLeft = manacher(s, n);
    // maxRight[i] := the maximum odd length of palindromes in s[i..n - 1]
    int[] maxRight = manacher(new StringBuilder(s).reverse().toString(), n);

    reverse(maxRight, 0, n - 1);

    for (int i = 1; i < n; ++i)
      ans = Math.max(ans, (long) maxLeft[i - 1] * maxRight[i]);

    return ans;
  }

  private int[] manacher(final String s, int n) {
    int[] maxExtends = new int[n];
    int[] leftToRight = new int[n];
    Arrays.fill(leftToRight, 1);
    int center = 0;

    for (int i = 0; i < n; ++i) {
      final int r = center + maxExtends[center] - 1;
      final int mirrorIndex = center - (i - center);
      int extend = i > r ? 1 : Math.min(maxExtends[mirrorIndex], r - i + 1);
      while (i - extend >= 0 && i + extend < n && s.charAt(i - extend) == s.charAt(i + extend)) {
        leftToRight[i + extend] = 2 * extend + 1;
        ++extend;
      }
      maxExtends[i] = extend;
      if (i + maxExtends[i] >= r)
        center = i;
    }

    for (int i = 1; i < n; ++i)
      leftToRight[i] = Math.max(leftToRight[i], leftToRight[i - 1]);

    return leftToRight;
  }

  private void reverse(int[] arr, int l, int r) {
    while (l < r)
      swap(arr, l++, r--);
  }

  private void swap(int[] arr, int i, int j) {
    final int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
  }
}
// code provided by PROGIEZ

1960. Maximum Product of the Length of Two Palindromic Substrings LeetCode Solution in Python

class Solution:
  def maxProduct(self, s: str) -> int:
    n = len(s)

    def manacher(s: str) -> list[int]:
      maxExtends = [0] * n
      leftToRight = [1] * n
      center = 0

      for i in range(n):
        r = center + maxExtends[center] - 1
        mirrorIndex = center - (i - center)
        extend = 1 if i > r else min(maxExtends[mirrorIndex], r - i + 1)
        while i - extend >= 0 and i + extend < n and s[i - extend] == s[i + extend]:
          leftToRight[i + extend] = 2 * extend + 1
          extend += 1
        maxExtends[i] = extend
        if i + maxExtends[i] >= r:
          center = i

      for i in range(1, n):
        leftToRight[i] = max(leftToRight[i], leftToRight[i - 1])

      return leftToRight

    # maxLeft[i] := the maximum odd length of palindromes in s[0..i]
    maxLeft = manacher(s)
    # maxRight[i] := the maximum odd length of palindromes in s[i..n - 1]
    maxRight = manacher(s[::-1])[::-1]
    return max(maxLeft[i - 1] * maxRight[i] for i in range(1, n))
# code by PROGIEZ

Additional Resources

See also  2899. Last Visited Integers LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.