首页 > 代码库 > Palindrome Partitioning
Palindrome Partitioning
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"] ]
public class Solution { public List<List<String>> partition(String s) { int len = s.length(); List<List<String>>[] result = new List[len + 1]; result[0] = new ArrayList<List<String>>(); result[0].add(new ArrayList<String>()); boolean[][] pair = new boolean[len][len]; for(int i=0; i<s.length(); i++){ result[i+1] = new ArrayList<List<String>>(); char c = s.charAt(i); for(int j=0; j<=i; j++){ if(j == i) pair[j][i] = true; else{ if(s.charAt(j) != c) continue; if(j == i-1) pair[j][i] = true; else pair[j][i] = pair[j+1][i-1]; } if(pair[j][i]){ String str = s.substring(j, i+1); for(List<String> r : result[j]){ List<String> ri = new ArrayList<String>(r); ri.add(str); result[i+1].add(ri); } } } } return result[len]; }}
Palindrome Partitioning
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。