131. Palindrome Partitioning

class Solution {
    public List<List<String>> partition(String s) {
        List<List<String>> res=new ArrayList<List<String>>();
        generatePartition(0,new ArrayList<String>(),res,s);
        return res;
    }
    private void generatePartition(int idx, List<String> list, List<List<String>> res, String s){
        if(idx==s.length())
        {
            res.add(new ArrayList<String>(list));
            return;
        }
        for(int i=idx;i<s.length();i++)
            if(isPalindrome(idx,i,s))
            {
                list.add(s.substring(idx,i+1));
                generatePartition(i+1,list,res,s);
                list.remove(list.size()-1);
            }
    }
    private boolean isPalindrome(int i, int j, String s){
        while(i<j&&s.charAt(i)==s.charAt(j))
        {
            i++;
            j--;
        }
        return i>=j;
    }
}

  

posted @ 2017-10-05 01:43  Weiyu Wang  阅读(117)  评论(0编辑  收藏  举报