LeetCode 3504: Longest Palindrome After Substring Concatenation II Solution

Master LeetCode problem 3504 (Longest Palindrome After Substring Concatenation II), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3504. Longest Palindrome After Substring Concatenation II

Problem Explanation

Explanation

To find the longest palindrome that can be formed by concatenating substrings from strings s and t, we need to consider all possible combinations of substrings from both strings. We can do this by iterating through all possible substrings of s and t, concatenating them, and checking if the resulting string is a palindrome.

For each pair of substrings, we can check if the concatenated string is a palindrome by comparing characters from the start and end of the string. If all characters match, then the concatenated string is a palindrome.

The time complexity of this approach is O(n^4) where n is the length of strings s and t since we are iterating through all possible substrings. The space complexity is O(1) as we are not using any extra space.

Solution Code

class Solution {
    public int longestPalindrome(String s, String t) {
        int maxLen = 0;
        for (int i = 0; i <= s.length(); i++) {
            for (int j = 0; j <= t.length(); j++) {
                for (int len = 0; i + len <= s.length() && j + len <= t.length(); len++) {
                    String subS = s.substring(i, i + len);
                    String subT = t.substring(j, j + len);
                    String concat = subS + subT;
                    if (isPalindrome(concat)) {
                        maxLen = Math.max(maxLen, concat.length());
                    }
                }
            }
        }
        return maxLen;
    }

    private boolean isPalindrome(String str) {
        int left = 0;
        int right = str.length() - 1;
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3504 (Longest Palindrome After Substring Concatenation II)?

This page provides optimized solutions for LeetCode problem 3504 (Longest Palindrome After Substring Concatenation II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3504 (Longest Palindrome After Substring Concatenation II)?

The time complexity for LeetCode 3504 (Longest Palindrome After Substring Concatenation II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3504 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3504 in Java, C++, or Python.

Back to LeetCode Solutions