大佬们的思路:利用dfs,对于i位置的的字符,两个子序列可以选择用或者不用;
class Solution {
int res=0;
public int maxProduct(String s) {
dfs(s,"","",0);
return res;
}
public void dfs(String s,String s1,String s2,int index){
if(check(s1)&&check(s2)){
res=Math.max(res,s1.length()*s2.length());//比较大小
}
if(index==s.length())return;//当前index已到达末尾
dfs(s,s1+s.charAt(index),s2,index+1);//s1使用index位置的字符
dfs(s,s1,s2+s.charAt(index),index+1);//s2使用index位置的字符
dfs(s,s1,s2,index+1);//s1,s2都不使用index位置的字符
}
public boolean check(String s){//判断是否为回文串
int len=s.length();
for(int i=0;i<len/2;i++){
if(s.charAt(i)!=s.charAt(len-i-1))
return false;
}
return true;
}
}