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. 冲刺Two之站立会议6

    今天继续了昨天的工作,对视频进行优化.因为昨天的工作没有达到预期的效果,所以又继续对音质和画面质量做了相应的优化.还对相应的聊天室界面进行了优化.

  2. 团队项目设计完善&编码测试

    任务1:软件设计方案说明书 <基于弹幕评论的大数据分析平台软件设计方案说明书>仓库链接:点击跳转 任务2:搭建并配置项目集成开发环境: 开发环境 java version "1. ...

  3. Alpha 冲刺八

    团队成员 051601135 岳冠宇 051604103 陈思孝 031602629 刘意晗 031602248 郑智文 031602234 王淇 会议照片 项目燃尽图 项目进展 完善各自部分 项目描 ...

  4. MacOS 如何剪切文件

    MacOS 如何剪切文件 MacOS 剪切文件 command + C 复制 command + V 粘贴 删除 & 粘贴 在 Windows中 Ctrl + X 是剪切,MacOS中没有剪切 ...

  5. Json序列化循环引用的问题

    今天在发布接口的时候出突然出现了一个问题,报错代码为: 1 An exception has occurred while using the formatter 'JsonMediaTypeForm ...

  6. codeforces1A

    Theatre Square CodeForces - 1A 一个城市的广场面积有 N×M平方米,过段时间,恰逢这个城市的庆典活动,主办方决定在广场上铺设一种新的地砖,这种地砖每块都是a×a平方米的. ...

  7. Cheerleaders UVA - 11806(容斥+二进制技巧)

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

  8. hdu 6301 Distinct Values (2018 Multi-University Training Contest 1 1004)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. 【BZOJ1037】[ZJOI2008]生日聚会(动态规划)

    [BZOJ1037][ZJOI2008]生日聚会(动态规划) 题面 BZOJ 洛谷 题解 假设前面的都合法,但是在加完当前的最后一个人之后变得不合法了,那么意味着一定有着一个后缀不合法.把男生看成\( ...

  10. Python 线程同步

    #-*-coding:utf-8-*- '''如果多个线程共同对某个数据修改,则可能出现不可预料的结果,为了保证数据的正确性, 需要对多个线程进行同步. 线程同步所使用的的方法: Lock RLock ...