Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example
Given s = "aab"
, return:
[
["aa","b"],
["a","a","b"]
]
DFS。想成每次从允许的index开始,for循环试试切割到某个位置拿出字符串来。如果切出不是回文的肯定不合格,放弃。如果切出的是回文的,那递归检查后面的substring们。如果切出来的正好切完最后一个字母了,那之前切法就是合格的,出口了放入result。
这里有一个可以优化的地方,就是提前把boolean[][] isPalindrome 数组准备好,之后就可以在重复寻求判断的时候O(1)而不是O(string长度)拿到结果。进一步优化创造boolean[][]数组的方法是动态规划,isPalindrome[i][j] = isPalindrome[i+1][j-1] && s.charAt(i) == s.charAt(j),每个
1. 普通准备boolean[][]数字
public class Solution { /* * @param s: A string * @return: A list of lists of string */ public List<List<String>> partition(String s) { // write your code here List<List<String>> result = new ArrayList<>(); if (s == null) { return result; } if (s.length() == 0) { List<String> list = new ArrayList<>(); list.add(""); result.add(list); return result; } boolean[][] isPalindromes = new boolean[s.length()][s.length()]; for (int i = 0; i < s.length(); i++) { for (int j = i; j < s.length(); j++) { isPalindromes[i][j] = isPalindrome(s.substring(i, j+1)); } } dfs(s, 0, isPalindromes, new ArrayList<String>(), result); return result; } private void dfs(String s, int idx, boolean[][] isPalindromes, List<String> crt, List<List<String>> result) { if (idx == s.length()) { result.add(new ArrayList<String>(crt)); return; } for (int i = idx; i < s.length(); i++) { if (!isPalindromes[idx][i]) { continue; } crt.add(s.substring(idx, i + 1)); dfs(s, i + 1, isPalindromes, crt, result); crt.remove(crt.size() - 1); } } private boolean isPalindrome(String s) { for (int i = 0, j = s.length() - 1; i < j; i++, j--) { if (s.charAt(i) != s.charAt(j)) { return false; } } return true; } }
2.动态规划准备boolean[][]数组(最好纸上画一下ij的矩阵,来判断下要利用boolean[i+1][j-1]的话要先准备哪些数据,之后要按什么顺序来算。实际上是先准备对角线[i][i]一条,对角线右侧[i][i+1]一条,之后要让i从下往上,j从左到右来算。)
public class Solution { /* * @param s: A string * @return: A list of lists of string */ public List<List<String>> partition(String s) { // write your code here List<List<String>> result = new ArrayList<>(); if (s == null) { return result; } if (s.length() == 0) { List<String> list = new ArrayList<>(); list.add(""); result.add(list); return result; } boolean[][] isPalindromes = isPalindromes(s); dfs(s, 0, isPalindromes, new ArrayList<String>(), result); return result; } private void dfs(String s, int idx, boolean[][] isPalindromes, List<String> crt, List<List<String>> result) { if (idx == s.length()) { result.add(new ArrayList<String>(crt)); return; } for (int i = idx; i < s.length(); i++) { if (!isPalindromes[idx][i]) { continue; } crt.add(s.substring(idx, i + 1)); dfs(s, i + 1, isPalindromes, crt, result); crt.remove(crt.size() - 1); } } private boolean[][] isPalindromes(String s) { boolean[][] isPalindromes = new boolean[s.length()][s.length()]; for (int i = 0; i < s.length(); i++) { isPalindromes[i][i] = true; } for (int i = 0; i < s.length() - 1; i++) { isPalindromes[i][i + 1] = s.charAt(i) == s.charAt(i + 1); } for (int i = s.length() - 3; i >= 0; i--) { for (int j = i + 2; j < s.length(); j++) { isPalindromes[i][j] = isPalindromes[i + 1][j - 1] && s.charAt(i) == s.charAt(j); } } return isPalindromes; } }