字符串中所有的回文子串

 

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

Return all possible palindrome partitioning of s.

For example, given s ="aab",
Return

  [
    ["aa","b"],
    ["a","a","b"]
  ]

import java.util.ArrayList;
import java.util.List;
public class Solution {
    public ArrayList<ArrayList<String>> partition(String s) {
        //lists 里面存放的是list
        ArrayList<ArrayList<String>> lists = new ArrayList<>();
        //每一个list存放的是一组分解的回文字符串
        ArrayList<String> list = new ArrayList<>();
        partitionHepler(lists,list,s);
        return lists;
    }
    
    public static void partitionHepler(ArrayList<ArrayList<String>> lists, ArrayList<String>list, String s){
        if(null == s || s.length() == 0){
            lists.add(new ArrayList<>(list));
            return;
        }
        //len存放字符串s的长度
        int len = s.length();
        //用for循环来控制第一个子字符串的长度
        for(int i = 0; i <= len; i++){
            //substring得到字符串的子字符串用subStr储存,0是子字符串的开始位置,i是结束位置
            String subStr = s.substring(0,i);
            //调用函数isPalindrome用来判断子字符串是否为回文
            if(isPalindrome(subStr)){
                //如果子字符串是回文,则添加到list中
                list.add(subStr);
                //递归调用partitionHepler函数,此时传入的s是从第一个回文数的结束位置到字符串的尾部
                partitionHepler(lists,list,s.substring(i,len));
                //删除list中最后一个元素
                list.remove(list.size() - 1);
            }
        }
    }
    
    public static boolean isPalindrome(String s){
        if(null == s || s.length() == 0)
            return false;
        int length = s.length();
        int middle = length/2;
        for(int i = 0; i < middle; i++){
            if(s.charAt(i) != s.charAt(length - i -1)){
                return false;
            }
        }
        return true;
    }
}

 

-----------------------------------------------------------------------6月27日修改--------------------------------------------------------------------------------------

 

package algorithm;

import java.util.ArrayList;
import java.util.Scanner;

/*待解决问题:字符串分解时最后一个字母不分解
 * 输出时不会输出所有可能的子list
 * 
 * 原因是isPalindrome函数中,判断条件问题。
 * 当字符串长度为1是middle算出来等于0
 * 
 */

public class StringPalindrome {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //String s = scan.next();
        String s = "aab";
        //System.out.println(s);
        StringPalindrome aa = new StringPalindrome();
        
        ArrayList<ArrayList<String>> lists = aa.palindrome(s);
        System.out.println(lists.toString());
        //System.out.println("test");
        
    }
    
    public ArrayList<ArrayList<String>> palindrome(String s){
        ArrayList<ArrayList<String>> lists = new ArrayList<>();
        
        ArrayList<String> list = new ArrayList<>();
        partition(lists,list,s);
        
        //System.out.println("test"+lists.toString());
        
        return lists; 
    }
    
    public static void partition(ArrayList<ArrayList<String>> lists, ArrayList<String> list, String s) {
        if(s == null || s.length() == 0) {
            lists.add(new ArrayList<>(list));
            return;
        }
        
        //System.out.println("test32" + s);
        
        int len = s.length();
        for(int i = 0; i <= len; i++) {
            String subStr = s.substring(0,i);    //开始位置包含,结束位置不包含,当开始位置和结束位置相同时不能取出字符串
            /*
             Returns a string that is a substring of this string. 
             Thesubstring begins at the specified beginIndex andextends to the character at index endIndex - 1.
             Thus the length of the substring is endIndex-beginIndex. 
            Examples: 

             "hamburger".substring(4, 8) returns "urge"
             "smiles".substring(1, 5) returns "mile"
 
            Parameters:beginIndex the beginning index, inclusive.
            endIndex the ending index, exclusive.Returns:the specified substring.
            Throws:IndexOutOfBoundsException - if the beginIndex is negative,
             or endIndex is larger than the length ofthis String object, or beginIndex is larger than endIndex.
             */
            
            //System.out.println("test1" + subStr);
            
            if(isPalindrome(subStr)) {
                //System.out.print("test2" + isPalindrome(subStr));
                list.add(subStr);
                partition(lists,list,s.substring(i,len));
                list.remove(list.size()-1);
                //System.out.println("test2" + list.toString());
                
            }
        }
    }
    public static boolean isPalindrome(String s) {
        if(s == null || s.length() == 0)
            return false;
        int length = s.length();
        int middle = s.length()/2;
        for(int i = 0; i < middle; i++) {
            //如果判断条件改成 == return true 计算结果失败(当被判断字符串为空或者只有一个字符的时候无法判断middle等于0)
            if(s.charAt(i) == s.charAt(length - i - 1)) {
                return true;
            }
        }
        return false;
    }
}

 

用到的方法

 

posted @ 2019-06-26 20:43  DIAO葫芦娃  阅读(2444)  评论(0编辑  收藏  举报