LeetCode 题解工作台
分割两个字符串得到回文串
给你两个字符串 a 和 b ,它们长度相同。请你选择一个下标,将两个字符串都在 相同的下标 分割开。由 a 可以得到两个字符串: a prefix 和 a suffix ,满足 a = a prefix + a suffix ,同理,由 b 可以得到两个字符串 b prefix 和 b suffix…
2
题型
6
代码语言
3
相关题
当前训练重点
中等 · 双·指针·invariant
答案摘要
我们可以使用双指针,其中一个指针 从字符串 的头部开始,另一个指针 从字符串 的尾部开始,如果两个指针指向的字符相等,那么两个指针同时往中间移动,直到遇到不同的字符或两指针交叉。 如果两指针交叉,即 $i \geq j$,说明 和 已经可以得到回文串,返回 `true`;否则,我们还需要判断 或者 是否是回文串,若是,返回 `true`。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 双·指针·invariant 题型思路
题目描述
给你两个字符串 a 和 b ,它们长度相同。请你选择一个下标,将两个字符串都在 相同的下标 分割开。由 a 可以得到两个字符串: aprefix 和 asuffix ,满足 a = aprefix + asuffix ,同理,由 b 可以得到两个字符串 bprefix 和 bsuffix ,满足 b = bprefix + bsuffix 。请你判断 aprefix + bsuffix 或者 bprefix + asuffix 能否构成回文串。
当你将一个字符串 s 分割成 sprefix 和 ssuffix 时, ssuffix 或者 sprefix 可以为空。比方说, s = "abc" 那么 "" + "abc" , "a" + "bc" , "ab" + "c" 和 "abc" + "" 都是合法分割。
如果 能构成回文字符串 ,那么请返回 true,否则返回 false 。
注意, x + y 表示连接字符串 x 和 y 。
示例 1:
输入:a = "x", b = "y" 输出:true 解释:如果 a 或者 b 是回文串,那么答案一定为 true ,因为你可以如下分割: aprefix = "", asuffix = "x" bprefix = "", bsuffix = "y" 那么 aprefix + bsuffix = "" + "y" = "y" 是回文串。
示例 2:
输入:a = "xbdef", b = "xecab" 输出:false
示例 3:
输入:a = "ulacfd", b = "jizalu" 输出:true 解释:在下标为 3 处分割: aprefix = "ula", asuffix = "cfd" bprefix = "jiz", bsuffix = "alu" 那么 aprefix + bsuffix = "ula" + "alu" = "ulaalu" 是回文串。
提示:
1 <= a.length, b.length <= 105a.length == b.lengtha和b都只包含小写英文字母
解题思路
方法一:双指针
我们可以使用双指针,其中一个指针 从字符串 的头部开始,另一个指针 从字符串 的尾部开始,如果两个指针指向的字符相等,那么两个指针同时往中间移动,直到遇到不同的字符或两指针交叉。
如果两指针交叉,即 ,说明 和 已经可以得到回文串,返回 true;否则,我们还需要判断 或者 是否是回文串,若是,返回 true。
否则,我们尝试交换两个字符串 和 ,重复上述同样的过程。
时间复杂度 ,空间复杂度 。其中 是字符串 或 的长度。
class Solution:
def checkPalindromeFormation(self, a: str, b: str) -> bool:
def check1(a: str, b: str) -> bool:
i, j = 0, len(b) - 1
while i < j and a[i] == b[j]:
i, j = i + 1, j - 1
return i >= j or check2(a, i, j) or check2(b, i, j)
def check2(a: str, i: int, j: int) -> bool:
return a[i : j + 1] == a[i : j + 1][::-1]
return check1(a, b) or check1(b, a)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) per split check due to the two-pointer scan, but only up to two main checks are needed, yielding overall O(n). Space complexity is O(1) since no additional structures are required beyond pointers and loop variables. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Look for a two-pointer solution rather than brute-force concatenation.
- question_mark
Early termination on palindrome detection shows understanding of invariants.
- question_mark
Attention to prefix-suffix mismatch handling indicates mastery of the problem pattern.
常见陷阱
外企场景- error
Assuming all split positions must be checked fully rather than using two-pointer early termination.
- error
Failing to handle empty prefix or suffix at string boundaries.
- error
Not checking both a_prefix + b_suffix and b_prefix + a_suffix combinations for each split.
进阶变体
外企场景- arrow_right_alt
Strings containing uppercase letters or Unicode characters instead of lowercase English letters.
- arrow_right_alt
Allowing multiple splits with the sum of lengths from each string forming a palindrome.
- arrow_right_alt
Limiting splits to a specific index range or requiring fixed-length prefixes.