Skip to content

Practice Maximize Palindrome Length From Subsequences

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1771
LeetCode Problem

Maximize Palindrome Length From Subsequences

Problem Statement

Choose a nonempty subsequence of word1 and a nonempty subsequence of word2, preserving the order of letters within each. Concatenate the first subsequence followed by the second. Return the greatest length obtainable when the concatenation is a palindrome, or 0 if no such palindrome exists.

Source: Maximize Palindrome Length From Subsequences on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
word1 = "cacb", word2 = "cbba"
Output
5

# Example 2

Input
word1 = "ab", word2 = "ab"
Output
3

# Example 3

Input
word1 = "aa", word2 = "bb"
Output
0

Constraints

1 <= word1.length, word2.length <= 1000
word1 and word2 consist of lowercase English letters.

Related LeetCode Problems

Practice more problems similar to Maximize Palindrome Length From Subsequences.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.