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. windows8/10+Ubuntu Kylin(优麒麟)双系统

    1.参考资料:http://www.jianshu.com/p/2eebd6ad284d 中第三种U盘启动方式安装完成 2.安装过程: (1)首先将一个盘空出来,做好其中数据的备份.启动win+X磁盘 ...

  2. ibmv7000查看序列号

    ssh后 命令:lsenclosure 有以下数据 id status   type      managed IO_group_id IO_group_name product_MTM serial ...

  3. CentOS 简单学习 firewalld的使用

    1. centos7 开始 使用firewalld 代替了 iptables 命令工具为 firewall-cmd 帮助信息非常长,简单放到文末 2. 简单使用 首先开启 httpd 一般都自带安装了 ...

  4. 个推数据统计产品(个数)iOS集成实践

    最近业务方给我们部门提了新的需求,希望能一站式统计APP的几项重要数据.这次我们尝试使用的是个推(之前专门做消息推送的)旗下新推出的产品“个数·应用统计”,根据官方的说法,个推的数据统计产品通过专业的 ...

  5. CentOS 7.4 java验证码乱码的问题

     转载阿里云 摘要: 新服务器配置发布网站 配置后程序顺利启动在登录时发现验证码无法识别显示出了图片,但是字是乱码 初步估计应该是字体问题 ssh登录服务器查看默认字体 #fc-match msam1 ...

  6. CentOS 6.8下安装python的redis支持库

    方法很简单,SSH登录下输入: pip install redis 或者 easy_install redis 如果上面的方法不行的话,就要尝试编译安装了 wget https://pypi.pyth ...

  7. HTML的自定义属性

    用Angular这些框架的时候会发现一系列的指令,如ng-app.ng-repeat等,这些都属于用户自定义属性 但是HTML5规范要求所有的用户自定义属性以"data-"开头,如 ...

  8. 在“安装”阶段发生异常。 System.Security.SecurityException: 未找到源,但未能

    写Windows服务的时候,运行了一下,就是没反应,命令框一闪而过,查了一下异常,大致是题目的那样.原因是因为权限不足.但是在网上搜的方法都不顶用. 解决方法如下: (1)以管理员身份运行CMD: ( ...

  9. Linux让git记住账号密码

    Linux让git记住账号密码 ——IT唐伯虎 摘要: Linux让git记住账号密码. 1.进入根目录,指令:cd / 2.创建记录账号密码的文件,指令:touch .git-credentials ...

  10. Oracle笔记 - unfinished

    1. plsql查看xmltype字段的xml格式时,出现中文乱码问题,可通过该字段.getClobVal():查询到的xml将是中文不乱码的. 2. extract函数查询xml某节点下的所有节点, ...