字符串中所有的回文子串


 

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;
    }
}

 

用到的方法

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM