LeetCode 题解工作台
子字符串连接后的最长回文串 I
给你两个字符串 s 和 t 。 你可以从 s 中选择一个子串(可以为空)以及从 t 中选择一个子串(可以为空),然后将它们 按顺序 连接,得到一个新的字符串。 返回可以由上述方法构造出的 最长 回文串的长度。 回文串 是指正着读和反着读都相同的字符串。 子字符串 是指字符串中的一个连续字符序列。 示…
4
题型
5
代码语言
3
相关题
当前训练重点
中等 · 状态·转移·动态规划
答案摘要
根据题目描述,连接后的回文串,可以只由字符串 组成,也可以只由字符串 组成,也可以由字符串 和字符串 组成,并且还可能在字符串 或 中多出一部分回文子串。 因此,我们先将字符串 反转,然后预处理出数组 和 ,其中 表示在字符串 中以下标 开始的最长回文子串长度,而 表示在字符串 中以下标 开始的最长回文子串长度。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你两个字符串 s 和 t。
你可以从 s 中选择一个子串(可以为空)以及从 t 中选择一个子串(可以为空),然后将它们 按顺序 连接,得到一个新的字符串。
返回可以由上述方法构造出的 最长 回文串的长度。
回文串 是指正着读和反着读都相同的字符串。
子字符串 是指字符串中的一个连续字符序列。
示例 1:
输入: s = "a", t = "a"
输出: 2
解释:
从 s 中选择 "a",从 t 中选择 "a",拼接得到 "aa",这是一个长度为 2 的回文串。
示例 2:
输入: s = "abc", t = "def"
输出: 1
解释:
由于两个字符串的所有字符都不同,最长的回文串只能是任意一个单独的字符,因此答案是 1。
示例 3:
输入: s = "b", t = "aaaa"
输出: 4
解释:
可以选择 "aaaa" 作为回文串,其长度为 4。
示例 4:
输入: s = "abcde", t = "ecdba"
输出: 5
解释:
从 s 中选择 "abc",从 t 中选择 "ba",拼接得到 "abcba",这是一个长度为 5 的回文串。
提示:
1 <= s.length, t.length <= 30s和t仅由小写英文字母组成。
解题思路
方法一:枚举回文中点 + 动态规划
根据题目描述,连接后的回文串,可以只由字符串 组成,也可以只由字符串 组成,也可以由字符串 和字符串 组成,并且还可能在字符串 或 中多出一部分回文子串。
因此,我们先将字符串 反转,然后预处理出数组 和 ,其中 表示在字符串 中以下标 开始的最长回文子串长度,而 表示在字符串 中以下标 开始的最长回文子串长度。
那么我们可以初始化答案 为 和 中的最大值。
接下来,我们定义 表示以字符串 的第 个字符结尾,以字符串 的第 个字符结尾的回文子串的长度。
对于 ,如果 等于 ,那么有 。然后,我们更新答案:
\textit{ans} = \max(\textit{ans}, \textit{f}[i][j] \times 2 + (0 \text{ if } i \geq m \text{ else } \textit{g1}[i])) \\ \textit{ans} = \max(\textit{ans}, \textit{f}[i][j] \times 2 + (0 \text{ if } j \geq n \text{ else } \textit{g2}[j])) \最后,我们返回答案 即可。
时间复杂度 ,空间复杂度 。其中 和 分别是字符串 和 的长度。
class Solution:
def longestPalindrome(self, s: str, t: str) -> int:
def expand(s: str, g: List[int], l: int, r: int):
while l >= 0 and r < len(s) and s[l] == s[r]:
g[l] = max(g[l], r - l + 1)
l, r = l - 1, r + 1
def calc(s: str) -> List[int]:
n = len(s)
g = [0] * n
for i in range(n):
expand(s, g, i, i)
expand(s, g, i, i + 1)
return g
m, n = len(s), len(t)
t = t[::-1]
g1, g2 = calc(s), calc(t)
ans = max(*g1, *g2)
f = [[0] * (n + 1) for _ in range(m + 1)]
for i, a in enumerate(s, 1):
for j, b in enumerate(t, 1):
if a == b:
f[i][j] = f[i - 1][j - 1] + 1
ans = max(ans, f[i][j] * 2 + (0 if i >= m else g1[i]))
ans = max(ans, f[i][j] * 2 + (0 if j >= n else g2[j]))
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n^2 * m^2) in the worst case where n and m are lengths of s and t due to substring enumeration, but DP and memoization reduce repeated work. Space complexity is O(n * m) for storing DP states. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidate considers all substring concatenations and identifies overlapping subproblems.
- question_mark
Candidate correctly implements state transition DP rather than naive brute force.
- question_mark
Candidate can explain why two pointers validate palindrome expansion efficiently.
常见陷阱
外企场景- error
Attempting pure brute force without DP leads to excessive computation.
- error
Ignoring empty substring cases causes off-by-one errors in palindrome length.
- error
Failing to track palindrome expansion across s and t boundaries results in undercounted lengths.
进阶变体
外企场景- arrow_right_alt
Allowing multiple concatenations of substrings instead of just one.
- arrow_right_alt
Restricting palindrome formation to contiguous concatenation only.
- arrow_right_alt
Finding the actual palindrome string, not just its length.