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. ElasticSearch 2 (15) - 深入搜索系列之多字段搜索

    ElasticSearch 2 (15) - 深入搜索系列之多字段搜索 摘要 查询很少是简单的一句话匹配(one-clause match)查询.很多时候,我们需要用相同或不同的字符串查询1个或多个字 ...

  2. DHCP分配固定IP

    https://www.cnblogs.com/liu1026/p/9829337.html 按照上面的操作实验好后在DHCP服务端的配置文件中加入

  3. Beta冲刺预备

    作业链接 Beta冲刺随笔集 github地址 讨论组长是否重选的议题和结论 在Alpha阶段我们由于没有项目经验,很多技术都仅限于书本上的知识,没有真正实践过,所以出现各种各样的问题,在组长的带领下 ...

  4. final版本发布评价II

    其实我对技术上的问题了解不多,所以有些评语可能说的不对或者压根就没啥用.可直接忽略.请见谅. 1新蜂的俄罗斯方块,UI设计虽然给出了背景和颜色,但是感觉色彩对比也不好,模块之间也不协调.没有更多的说服 ...

  5. convert函数语法

    convert函数语法: CONVERT(data_type(length),  data_to_be_converted,  style)data_type(length) 规定目标数据类型(带有可 ...

  6. [转帖]Kubernetes及容器编排的总体介绍【译】

    Kubernetes及容器编排的总体介绍[译] 翻译自The New Stack<Kubernetes 生态环境>作者:JANAKIRAM MSV和 KRISHNAN SUBRAMANIA ...

  7. DevOps简介

    DevOps 是一个完整的面向IT运维的工作流,以 IT 自动化以及持续集成(CI).持续部署(CD)为基础,来优化程式开发.测试.系统运维等所有环节. DevOps的概念 DevOps一词的来自于D ...

  8. Hbase之IP变更后无法启动问题解决

    # 修改hbase IP配置文件地址:/opt/hbase-1.1.13/conf/hbase-site.xml <property> <name>hbase.zookeepe ...

  9. MySQL : 数据库和表的基本操作总结

    针对database和table的操作大致可分为三类:创建,查询,修改,删除 1. 创建 create ①创建数据库 create database db_name; ②创建表 create tabl ...

  10. UOJ#424 【集训队作业2018】count

    题意 我们定义长度为\(n\),每个数为\(1\sim m\)之间的整数且\(1\sim m\)都至少出现一次的序列为合法序列.再定义\(pos(l,r)\)表示这个序列的区间\([l,r]\)之间的 ...