Palindrome Partitioning I & II
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Given s = "aab", return:
[
["aa","b"],
["a","a","b"]
]
public class Solution {
public List<List<String>> partition(String str) {
List<List<String>> listAll = new ArrayList<>();
if (str== null || str.length() == ) return listAll;
for (int i = ; i <= str.length(); i++) {
String strPart1 = str.substring(, i);
String strPart2 = str.substring(i);
if (isPalindrome(strPart1)) {
if (strPart1.equals(str)) {
List<String> list = new ArrayList<>();
list.add(str);
listAll.add(list);
} else {
List<List<String>> temp = partition(strPart2);
for (int j = ; j < temp.size(); j++) {
temp.get(j).add(, strPart1);
listAll.add(new ArrayList<String>(temp.get(j)));
}
}
}
}
return listAll;
}
public boolean isPalindrome(String str) {
if (str == null || str.length() == ) return true;
int i = , j = str.length() - ;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
另一种方法:https://leetcode.com/problems/palindrome-partitioning/discuss/41982/Java-DP-%2B-DFS-solution
first, I ask myself that how to check if a string is palindrome or not, usually a two point solution scanning from front and back. Here if you want to get all the possible palindrome partition, first a nested for loop to get every possible partitions for a string, then a scanning for all the partitions. That's a O(n^2) for partition and O(n^2) for the scanning of string, totaling at O(n^4) just for the partition. However, if we use a 2d array to keep track of any string we have scanned so far, with an addition pair, we can determine whether it's palindrome or not by justing looking at that pair, which is this line if(s.charAt(i) == s.charAt(j) && (i - j <= 2 || dp[j+1][i-1])). This way, the 2d array dpcontains the possible palindrome partition among all.
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
boolean[][] dp = new boolean[s.length()][s.length()];
for (int i = ; i < s.length(); i++) {
for (int j = ; j <= i; j++) {
if (s.charAt(i) == s.charAt(j) && (i - j <= || dp[j + ][i - ])) {
dp[j][i] = true;
}
}
}
helper(res, new ArrayList<>(), dp, s, );
return res;
}
private void helper(List<List<String>> res, List<String> path, boolean[][] dp, String s, int pos) {
if (pos == s.length()) {
res.add(new ArrayList<>(path));
return;
}
for (int i = pos; i < s.length(); i++) {
if (dp[pos][i]) {
path.add(s.substring(pos, i + ));
helper(res, path, dp, s, i + );
path.remove(path.size() - );
}
}
}
}
Palindrome Partitioning II
Given a string s, cut s into some substrings such that every substring is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
Given s = "aab",
Return 1 since the palindrome partitioning ["aa", "b"] could be produced using 1 cut.
分析:
用cuts[i]表示从0 到 i最小的cuts数。那么为了得到cuts[i], 我们需要从0到i,看string 的k (0 <= k < i) 到 i部分是否是palindrome。是的话,我们需要更新当前cuts[i]的值,因为我们有可能在这个时候找到一个更小的值。
public class Solution {
public int minCut(String s) {
if (s == null || s.length() <= ) return ;
int[] cuts = new int[s.length()];
for (int i = ; i < cuts.length; i++) {
cuts[i] = i;
}
for (int i = ; i < cuts.length; i++) {
if (isPalindrome(s, , i)) {
cuts[i] = ;
continue;
}
for (int j = ; j <= i; j++) {
if (isPalindrome(s, j, i)) {
cuts[i] = Math.min(cuts[i], cuts[j - ] + );
}
}
}
return cuts[s.length() - ];
}
public boolean isPalindrome(String str, int start, int end) {
while(start < end) {
if (str.charAt(start) != str.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
}
Palindrome Partitioning I & II的更多相关文章
- leetcode之 Palindrome Partitioning I&II
1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...
- 【算法】leetcode之 Palindrome Partitioning I&II(转载)
1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...
- LeetCode 132. 分割回文串 II(Palindrome Partitioning II)
题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- 《Linux内核分析》课程第五周学习总结
姓名:何伟钦 学号:20135223 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/U ...
- Alpha 冲刺三
团队成员 051601135 岳冠宇 051604103 陈思孝 031602629 刘意晗 031602248 郑智文 031602234 王淇 会议照片 项目燃尽图 项目进展 发布界面布局完成.登 ...
- Beta 冲刺 一
团队成员 051601135 岳冠宇 031602629 刘意晗 031602248 郑智文 031602330 苏芳锃 031602234 王淇 照片 项目进展 岳冠宇 昨天的困难 无 今天的进度 ...
- 极简版 卸载 home 扩充 根分区--centos7 xfs 文件格式
1. 查看文件系统 df -Th 2. 关闭正常连接 /home的用户 fuser /home 3. 卸载 /home的挂载点 umount /home 4.删除home的lv 注意 lv的名称的写法 ...
- Git从零开始(二)
前面提交了一个test1.txt文件,接下来看看这个文件接下来的命运. 一.继续修改并提交 在test1.txt中修改后, git status 查看git的状态, 会提示我们文件test1.txt被 ...
- 安装 oracle
先下载3个东西:链接忘记了,大家自己找一下 1 ORA+11+G+R2+server+64bit+for+windows.iso (oracle 安装文件) 2 PLSql 3 oracle6 ...
- CF86D Powerful array
题意翻译 题意:给出一个n个数组成的数列a,有t次询问,每次询问为一个[l,r]的区间,求区间内每种数字出现次数的平方×数字的值 的和. 输入:第一行2个正整数n,t. 接下来一行n个正整数,表示数列 ...
- net 和Mono 构建的HTTP服务框架
Nancy是一个基于.net 和Mono 构建的HTTP服务框架,是一个非常轻量级的web框架. 设计用于处理 DELETE, GET, HEAD, OPTIONS, POST, PUT 和 PATC ...
- 51nod1134——(最长上升子序列)
给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行:1个 ...
- 内置函数sorted()
这里顺便说一下sorted()和sort()的异同. sort 是 list 中的方法,只能对列表排序:sorted 可以对所有可迭代对象进行排序. list 的 sort 方法是对原列表进行操作,而 ...