Palindrome II

Problem Statement

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s = "aab",  Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

 

First we need to give some definitions. Then

Define

  • cut[k] to indicate the minimum number of cuts of the first k characters in string s, {s0,s1,...,sk1}.

 

  • Thus, the index of cut is one more than index of s, which means the number of minimum cut of {s0,...,si} will be stored at cut[si], or cut[i+1]. For simplicity, we use the terminology minimum cut of si to indicate minimum cut of {s0,...,si}.

 

  • So the problem can be expressed to compute cut[n]. 

 

Obviously, the initial cut[i] should be i1. Because every node itself is a palindrome. Thus n nodes at most need n1 partitions:

1
2
for(int i = 0; i < n; ++i)
    cut[i] = i - 1;

 

At first sight, the assignment cut[0]=1 seems like a garbage value, but later we will explain it's very useful to assign cut[0] to -1 to complete our problem.

 

In order to find some intuitions, let's think about the minimum cut of string {s0,s1,...,si} with cut[i+1] partitions.

Denote the cut like: {s0,s1,...,si} = {P1,P2,...,Pk} =
{
{s0,s1,...,sn1},
{sn1+1,sn1+2,...,sn2},
{...},
....,
{snk2+1,snk2+2,...,snk1},
{snk1+1,snk1+2,...,snk}
}.

Immediately, we can get

  1. The node si must be in Pk with si=snk. And Pk is a palindrome.
  2. Also, consider the node snk1. We can state that {P1,P2,...,Pk1} is also the minimum cut of string {s0,s1,...,snk1}.
    (Otherwise, we can use {s0,s1,...,snk}'s minimum cut {P1,P2,...,Pk1} {Pk} to get {s0,s1,...,si}'s smaller cut, which contradicates to {P1,P2,...,Pk}'s minimum.)
  3. Based on the above two statements, we also get k=(k1)+1, which means cut[si]=cut[snk1]+1, or cut[i+1]=cut[nk1+1]+1.
  4. The situation that the whole string {s0,...,si} is palindrome, i.e., Pk= {s0,...,si}, Pk1= and cut[i]=0, is also included.
    As the deductions above, cut[i]=cut[s1]+1=cut[0]+1=1+1=0, which matches the result of our problem.
  5. Point 4 also explains that the initial assignment of cut[0] = -1 is useful and meaningful.

From these insights, we can get that for every minimum partition, let's say node si's minimum cut, there exists some j, where ji, such that

si's minimum cut =

{s0,s1,...,sj1}'s minimum cut



{sj,...,si}, where {sj,...,si} is a palindrome.

Thus, if we test a palindrome {sp,...,sq}, it may be the minimum partition's last part Pk. So we may have a chance to update sq's minimum cut, i.e. updating cut[q+1] by cut[sq]=min(cut[sq],cut[sp1]+1)

1
2
if(isPalindrome(s, p, q))
    cut[q+1] = max(cut[q+1], cut[p]+1)

 

As every node si's cut[i+1] can only be affected by its previous nodes, we can systematically detect palindrome and then update cut[i+1] from the beginning of string s.

1
2
3
4
for(int point = 0; point < n; ++point){
    //detect palindrome and update cut[i]
    ...
}

 

For every node si, we consider palindrome centralized at si. Of course, there may be two cases of palindrome centralized at si:

  • [sij,...,si,...,si+j] with odd length 2j+1.

 

  • [si(j1),...,si,si+1,...,si+j] with even length 2j

We can expand the half length j from zero until it reaches the boundary s0 or sn1. And once the expansion of length stops at some length j, there won't exist palindrome centralized at i with length greater than j.

1
2
3
4
5
6
7
// odd length
for(int halfLength = 0; point-halfLength >= 0 && point+halfLength < n && s[point-halfLength] == s[point+halfLength])
    cut[point+halfLength+1] = min(cut[point+halfLength+1], cut[point-halfLength]+1);
     
// even length
for(int halfLength = 1; point-halfLength+1 >= 0 && point+halfLength < n && s[point-halfLength+1] == s[point+halfLength])
    cut[point+halfLength+1] = min(cut[point+halfLength+1], cut[point-halfLength+1]+1)

or if we use i denote central point, and j denote half length, we can get a piece of simply code:

1
2
3
4
5
6
7
8
9
for (int i = 0; i < n; ++i){
    //odd length
    for (int j = 0; i-j >= 0 && i+j < n && s[i-j] == s[i+j]; ++j)
        cut[i+j+1] = min(cut[i+j+1], cut[i-j]+1);
 
    //even length
    for (int j = 1; i-j+1 >= 0 && i+j < n && s[i-j+1] == s[i+j]; ++j)
        cut[i+j+1] = min(cut[i+j+1], cut[i-j+1]+1);
}

 


The complete code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int minCut(string s) {
    int n = s.size();
    if(n <= 1) return 0;
 
    vector<int> cut(n+1, 0);
 
    for (int i = 0; i <= n; i++)
        cut[i] = i-1;
 
    for (int i = 0; i < n; ++i)
    {
        //odd length, i.e. i is the middle point, [i-j, ..., i, ..., i+j];
        for (int j = 0; i-j >= 0 && i+j < n && s[i-j] == s[i+j]; ++j)
            cut[i+j+1] = min(cut[i+j+1], cut[i-j]+1);
 
        //even length, i.e. i is left side's endpoint, [i-(j-1), ..., i, i+1, ..., i+j];
        for (int j = 1; i-j+1 >= 0 && i+j < n && s[i-j+1] == s[i+j]; ++j)
            cut[i+j+1] = min(cut[i+j+1], cut[i-j+1]+1);
    }
 
    return cut[n];
}

The running time is about two parts. The first is the assignment of cut[i], O(n).

The second part is divided by two for loop:

  • O(1+2+3+4+...+n/2)=O(n2) 
  • O(1+2+3+4+...+n/2)=O(n2)

So the total running time is O(n2).

And obviously the space is O(n).

posted @   kid551  阅读(119)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示