LeetCode 题解工作台

分割两个字符串得到回文串

给你两个字符串 a 和 b ,它们长度相同。请你选择一个下标,将两个字符串都在 相同的下标 分割开。由 a 可以得到两个字符串: a prefix 和 a suffix ,满足 a = a prefix + a suffix ,同理,由 b 可以得到两个字符串 b prefix 和 b suffix…

category

2

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 双·指针·invariant

bolt

答案摘要

我们可以使用双指针,其中一个指针 从字符串 的头部开始,另一个指针 从字符串 的尾部开始,如果两个指针指向的字符相等,那么两个指针同时往中间移动,直到遇到不同的字符或两指针交叉。 如果两指针交叉,即 $i \geq j$,说明 和 已经可以得到回文串,返回 `true`;否则,我们还需要判断 或者 是否是回文串,若是,返回 `true`。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 双·指针·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个字符串 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 <= 105
  • a.length == b.length
  • a 和 b 都只包含小写英文字母
lightbulb

解题思路

方法一:双指针

我们可以使用双指针,其中一个指针 ii 从字符串 aa 的头部开始,另一个指针 jj 从字符串 bb 的尾部开始,如果两个指针指向的字符相等,那么两个指针同时往中间移动,直到遇到不同的字符或两指针交叉。

如果两指针交叉,即 iji \geq j,说明 prefixprefixsuffixsuffix 已经可以得到回文串,返回 true;否则,我们还需要判断 a[i,...j]a[i,...j] 或者 b[i,...j]b[i,...j] 是否是回文串,若是,返回 true

否则,我们尝试交换两个字符串 aabb,重复上述同样的过程。

时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。其中 nn 是字符串 aabb 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
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)
speed

复杂度分析

指标
时间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
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

分割两个字符串得到回文串题解:双·指针·invariant | LeetCode #1616 中等