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"]
]
分析:
创建一个method partition 用于把一个string str分成多个palindrome. 怎么做呢?既然是分割,那么那个分割点就是在每两个字符中间。所以,先把str分成两段,[0, i] and [i + 1, n],看第一段是否是palindrome,是的话,递归调用partition,并且把第二段传进去。然后把得到的list和第一段合并。
 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.

Example

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的更多相关文章

  1. leetcode之 Palindrome Partitioning I&II

    1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...

  2. 【算法】leetcode之 Palindrome Partitioning I&II(转载)

    1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...

  3. LeetCode 132. 分割回文串 II(Palindrome Partitioning II)

    题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...

  4. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  6. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  7. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  8. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  9. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. Hadoop 4 MapReduce

    对单词个数统计的MapReduce的案例 Mapper类: package main.java.worldClient; import java.io.IOException; import org. ...

  2. [转帖]PG里面的Citus简介----找时间学习一下.

    1. Citus是什么 是PostgreSQL的扩展,可以同PG一同安装,之后通过SQL命令加入到数据库中. [相关操作] ? 1 2 #创建Citus扩展: CREATE EXTENSION cit ...

  3. dx、aapt工具

    1.DX命令: Dalvik虚拟机不能直接运行Java代码编译出来的文件,只能运行.dex文件,所以需要将.class,.jar等文件通过DX工具转换成.dex文件. 2.AAPT命令 一个Andro ...

  4. 【Java并发编程】之十六:深入Java内存模型——happen-before规则及其对DCL的分析(含代码)

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17348313 happen-before规则介绍 Java语言中有一个"先行发生 ...

  5. ubuntu系统创建新用户并赋予sudo权限

    1.创建新用户 创建新用户有两种方式:adduser和useradd adduser会为用户创建組./home目录下同名文件夹,密码,而useradd不会 因此推荐使用adduser创建用户,例: s ...

  6. BZOJ 4569 [Scoi2016]萌萌哒 | ST表 并查集

    传送门 BZOJ 4569 题解 ST表和并查集是我认为最优雅(其实是最好写--)的两个数据结构. 然鹅!他俩加一起的这道题,我却--没有做出来-- 咳咳. 正解是这样的: 类似ST表有\(\log ...

  7. 洛谷 P2376 [USACO09OCT]津贴Allowance 解题报告

    P2376 [USACO09OCT]津贴Allowance 题目描述 作为创造产奶纪录的回报,\(Farmer\) \(John\)决定开始每个星期给\(Bessie\)一点零花钱. \(FJ\)有一 ...

  8. luogu1081 [NOIp2012]开车旅行 (STL::multiset+倍增)

    先用不管什么方法求出来从每个点出发,A走到哪.B走到哪(我写了一个很沙雕的STL) 然后把每个点拆成两个点,分别表示A从这里出发和B从这里出发,然后连边是要A连到B.B连到A.边长就是这次走的路径长度 ...

  9. Mac OS X下:TensorBoard可视化问题

    花了1,2个小时,Tensorboard Garphs一直不显示,最后发现竟然是多了一个“=”号

  10. java基础基础总结----- System

    常用的方法: 细节分析: