

class Solution {
public boolean checkPartitioning(String s) {
int len = s.length();
int head = 0;
int tail = len-1;
int prehead = head;
int behead = head;
if(checkApart(head,tail,s))
return true;
while(head < tail)
{
prehead = head;
do
{
head++;
}while(head < tail && !checkHuiWen(s.substring(0,head+1)));
behead = head;
if(head < tail)
if(checkApart(head,tail,s))
return true;
head = prehead;
do
{
tail--;
}while(head < tail && !checkHuiWen(s.substring(tail,s.length())));
if(head < tail)
if(checkApart(head,tail,s))
return true;
head = behead;
if(head < tail)
if(checkApart(head,tail,s))
return true;
}
return false;
}
public boolean checkHuiWen(String str)
{
int n = str.length();
for(int i=0;i<n/2;i++)
if(str.charAt(i) != str.charAt(n-i-1))
return false;
return true;
}
public boolean checkApart(int head,int tail, String s)
{
return checkHuiWen(s.substring(0,head+1)) && checkHuiWen(s.substring(head+1,tail)) && checkHuiWen(s.substring(tail,s.length()));
}
}
本文介绍了一种判断字符串是否能通过三次分割形成四个回文子串的方法。算法使用双指针从两端向中间搜索可能的分割点,并验证分割后的子串是否为回文。此方法适用于需要高效解决字符串回文问题的场景。

3372

被折叠的 条评论
为什么被折叠?



