You can create a new string by selecting a substring from s (possibly empty) and a substring from t (possibly empty), then concatenating them in order.
Return the length of the longest palindrome that can be formed this way.
Input: s ="abcde", t ="ecdba"Output: 5Explanation:
Concatenating `"abc"` from `s` and `"ba"` from `t` results in`"abcba"`, which
is a palindrome of length 5.
To find the longest palindrome by concatenating substrings from s and t, we need to consider all possible ways to take a prefix from s and a suffix from t. For each such concatenation, we find the longest palindrome within that string using center expansion technique.
Generate all possible concatenations: For each possible prefix length i from s (0 to m) and suffix starting position j from t (0 to n), create the concatenated string s[0:i] + t[j:n].
Find longest palindrome in each concatenation: For each concatenated string, use the center expansion technique to find the longest palindrome:
Check for odd-length palindromes (center at single character)
Check for even-length palindromes (center between two characters)
Center expansion: For each potential center, expand outward while characters match, keeping track of the maximum palindrome length found.
Return maximum: Track and return the maximum palindrome length across all possible concatenations.
classSolution {
public:int longestPalindrome(string s, string t) {
int m = s.length(), n = t.length(), maxLen =0;
for (int i =0; i <= m; i++) {
for (int j =0; j <= n; j++) {
string concat = s.substr(0, i) + t.substr(j);
int len = concat.length();
for (int k =0; k < len; k++) {
// Check odd-length palindromes (center at k)
maxLen = max(maxLen, expandAroundCenter(concat, k, k));
// Check even-length palindromes (center between k and k+1)
maxLen = max(maxLen, expandAroundCenter(concat, k, k +1));
}
}
}
return maxLen;
}
private:int expandAroundCenter(const string& str, int left, int right) {
while (left >=0&& right < str.length() && str[left] == str[right]) {
left--;
right++;
}
return right - left -1;
}
};
classSolution {
funlongestPalindrome(s: String, t: String): Int {
val m = s.length
val n = t.length
var maxLen = 0for (i in0..m) {
for (j in0..n) {
val concat = s.substring(0, i) + t.substring(j)
val length = concat.length
for (k in0 until length) {
// Check odd-length palindromes
maxLen = maxOf(maxLen, expandAroundCenter(concat, k, k))
// Check even-length palindromes
maxLen = maxOf(maxLen, expandAroundCenter(concat, k, k + 1))
}
}
}
return maxLen
}
privatefunexpandAroundCenter(str: String, left: Int, right: Int): Int {
var l = left
var r = right
while (l >=0&& r < str.length && str[l] == str[r]) {
l-- r++ }
return r - l - 1 }
}
classSolution:
deflongestPalindrome(self, s: str, t: str) -> int:
m, n = len(s), len(t)
max_len =0for i in range(m +1):
for j in range(n +1):
concat = s[:i] + t[j:]
length = len(concat)
for k in range(length):
# Check odd-length palindromes max_len = max(max_len, self.expand_around_center(concat, k, k))
# Check even-length palindromes max_len = max(max_len, self.expand_around_center(concat, k, k +1))
return max_len
defexpand_around_center(self, s: str, left: int, right: int) -> int:
while left >=0and right < len(s) and s[left] == s[right]:
left -=1 right +=1return right - left -1
impl Solution {
pubfnlongest_palindrome(s: String, t: String) -> i32 {
let m = s.len();
let n = t.len();
letmut max_len =0;
for i in0..=m {
for j in0..=n {
let concat =format!("{}{}", &s[..i], &t[j..]);
let chars: Vec<char>= concat.chars().collect();
let length = chars.len();
for k in0..length {
// Check odd-length palindromes
max_len = max_len.max(Self::expand_around_center(&chars, k, k));
// Check even-length palindromes
if k +1< length {
max_len = max_len.max(Self::expand_around_center(&chars, k, k +1));
}
}
}
}
max_len asi32 }
fnexpand_around_center(chars: &[char], mut left: usize, mut right: usize) -> usize {
while left < chars.len() && right < chars.len() && chars[left] == chars[right] {
if left ==0 {
break;
}
left -=1;
right +=1;
}
if left < chars.len() && right < chars.len() && chars[left] == chars[right] {
right - left +1 } else {
right - left -1 }
}
}
⏰ Time complexity: O(n*m*(n+m)^2), where n and m are the lengths of s and t. We generate O(n*m) possible concatenations (each prefix of s with each suffix of t). For each concatenation of length up to (n+m), we check O(n+m) possible centers, and each center expansion takes O(n+m) time in the worst case.
🧺 Space complexity: O(n+m), for storing the concatenated strings during processing.