LeetCode 题解工作台

段式回文

你会得到一个字符串 text 。你应该把它分成 k 个子字符串 (subtext1, subtext2,…, subtextk) ,要求满足: subtext i 是 非空 字符串 所有子字符串的连接等于 text ( 即 subtext 1 + subtext 2 + ... + subtext …

category

6

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

我们可以从字符串的两端开始,寻找长度最短的、相同且不重叠的前后缀: - 如果找不到这样的前后缀,那么整个字符串作为一个段式回文,答案加 ;

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

你会得到一个字符串 text 。你应该把它分成 k 个子字符串 (subtext1, subtext2,…, subtextk) ,要求满足:

  • subtexti 非空 字符串
  • 所有子字符串的连接等于 text ( 即subtext1 + subtext2 + ... + subtextk == text )
  • 对于所有 i 的有效值( 即 1 <= i <= k ) ,subtexti == subtextk - i + 1 均成立

返回k可能最大值。

 

示例 1:

输入:text = "ghiabcdefhelloadamhelloabcdefghi"
输出:7
解释:我们可以把字符串拆分成 "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)"。

示例 2:

输入:text = "merchant"
输出:1
解释:我们可以把字符串拆分成 "(merchant)"。

示例 3:

输入:text = "antaprezatepzapreanta"
输出:11
解释:我们可以把字符串拆分成 "(a)(nt)(a)(pre)(za)(tep)(za)(pre)(a)(nt)(a)"。

 

提示:

  • 1 <= text.length <= 1000
  • text 仅由小写英文字符组成
lightbulb

解题思路

方法一:贪心 + 双指针

我们可以从字符串的两端开始,寻找长度最短的、相同且不重叠的前后缀:

  • 如果找不到这样的前后缀,那么整个字符串作为一个段式回文,答案加 11
  • 如果找到了这样的前后缀,那么这个前后缀作为一个段式回文,答案加 22,然后继续寻找剩下的字符串的前后缀。

以上贪心策略的证明如下:

假设有一个前后缀 A1A_1A2A_2 满足条件,又有一个前后缀 B1B_1B4B_4 满足条件,由于 A1=A2A_1 = A_2,且 B1=B4B_1=B_4,那么有 B3=B1=B4=B2B_3=B_1=B_4=B_2,并且 C1=C2C_1 = C_2,因此,如果我们贪心地将 B1B_1B4B_4 分割出来,那么剩下的 C1C_1C2C_2,以及 B2B_2B3B_3 也能成功分割。因此我们应该贪心地选择长度最短的相同前后缀进行分割,这样剩余的字符串中,将可能分割出更多的段式回文串。

时间复杂度 O(n2)O(n^2),空间复杂度 O(n)O(n)O(1)O(1)。其中 nn 为字符串的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
    def longestDecomposition(self, text: str) -> int:
        ans = 0
        i, j = 0, len(text) - 1
        while i <= j:
            k = 1
            ok = False
            while i + k - 1 < j - k + 1:
                if text[i : i + k] == text[j - k + 1 : j + 1]:
                    ans += 2
                    i += k
                    j -= k
                    ok = True
                    break
                k += 1
            if not ok:
                ans += 1
                break
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate demonstrates the ability to apply dynamic programming principles effectively.

  • question_mark

    The candidate shows understanding of optimization techniques like rolling hash for substring comparisons.

  • question_mark

    The candidate can combine multiple strategies, including two-pointer and greedy approaches, to solve the problem efficiently.

warning

常见陷阱

外企场景
  • error

    Not properly handling edge cases like very small strings or strings with no palindromes.

  • error

    Failing to optimize the palindrome check, leading to inefficiencies in time complexity.

  • error

    Not properly implementing the state transition dynamic programming approach, resulting in incorrect outputs.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    The problem can be modified to include constraints like requiring each substring to be of even length.

  • arrow_right_alt

    Allowing only certain types of palindromes (e.g., odd-length ones) introduces further complexity.

  • arrow_right_alt

    Changing the input string to a set of characters or more complex symbols could lead to a variation in the approach.

help

常见问题

外企场景

段式回文题解:状态·转移·动态规划 | LeetCode #1147 困难