Q1: 回文字符串的分割

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

For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
]

算法
回溯法.

  • 从字符串开头扫描, 找到一个下标i, 使得 str[0..i]是一个回文字符串
  • 将str[0..i]记入临时结果中
  • 然后对于剩下的字符串str[i+1, end]递归调用前面的两个步骤, 直到i+1 >= end结束
  • 这时候, 我们找到了一组结果.
  • 开始回溯. 以回溯到最开始的位置i为例. 从i开始, 向右扫描, 找到第一个位置j, 满足str[0..j]为一个回文字符串. 然后重复前面的四个步骤.

以字符串 "ababc" 为例.

  • 首先找到 i = 0, "a"为回文字符串.
  • 然后在子串"babc"中继续查找, 找到下一个 "b", 递归找到 "a", "b", "c". 至此我们找到了第一组结果. ["a", "b", "a", "b", "c"]
  • 将c从结果中移除, 位置回溯到下标为3的"b". 从"b"开始向后是否存在str[3..x]为回文字符串, 发现并没有.
  • 回溯到下标为2的"a", 查找是否存在str[2..x]为回文字符串, 发现也没有.
  • 继续回溯到下标为1的"b", 查找是否存在str[1..x]为回文字符串, 找到了"bab", 记入到结果中. 然后从下标为4开始继续扫描. 找到了下一个回文字符串"c".
  • 我们找到了下一组结果 ["a", "bab", "c"]
  • 然后继续回溯 + 递归.

实现

class Solution {
public:
vector<vector<string>> partition(string s) {
std::vector<std::vector<std::string> > results;
std::vector<std::string> res;
dfs(s, 0, res, results);
return results;
}
private:
void dfs(std::string& s, int startIndex,
std::vector<std::string> res,
std::vector<std::vector<std::string> >& results)
{
if (startIndex >= s.length())
{
results.push_back(res);
}
for (int i = startIndex; i < s.length(); ++i)
{
int l = startIndex;
int r = i;
while (l <= r && s[l] == s[r]) ++l, --r;
if (l >= r)
{
res.push_back(s.substr(startIndex, i - startIndex + 1));
dfs(s, i + 1, res, results);
res.pop_back();
}
}
}
};

  

Q2 回文字符串的最少分割数

Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s = "aab",
Return 1 since the palindrome partitioning
["aa","b"] could be produced using 1 cut.

算法
Calculate and maintain 2 DP states:

  • dp[i][j] , which is whether s[i..j] forms a pal
  • isPalindrome[i], which is the minCut for s[i..n-1]
  • Once we comes to a pal[i][j]==true:
    • if j==n-1, the string s[i..n-1] is a Pal, minCut is 0, d[i]=0;
    • else: the current cut num (first cut s[i..j] and then cut the rest s[j+1...n-1]) is 1+d[j+1], compare it to the exisiting minCut num d[i], repalce if smaller.
      d[0] is the answer.

实现

class Solution {

public:
int minCut(std::string s) {
int len = s.length();
int minCut = 0;
bool isPalindrome[len][len] = {false};
int dp[len + 1] = {INT32_MAX};
dp[len] = -1;
for (int leftIndex = len - 1; leftIndex >= 0; --leftIndex)
{
for (int midIndex = leftIndex; midIndex <= len - 1; ++midIndex)
{
if ((midIndex - leftIndex < 2 || isPalindrome[leftIndex + 1][midIndex -1])
&& s[leftIndex] == s[midIndex])
{
isPalindrome[leftIndex][midIndex] = true;
dp[leftIndex] = std::min(dp[midIndex + 1] + 1, dp[leftIndex]);
}
}
std::cout << leftIndex << ": " << dp[leftIndex] << std::endl;
}
return dp[0];
}
};

  

Leetcode. 回文字符串的分割和最少分割数的更多相关文章

  1. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  3. leetcode 5 Longest Palindromic Substring--最长回文字符串

    问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. [LeetCode] 680. Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  5. LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1

    680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...

  6. 【LeetCode】1400. 构造 K 个回文字符串 Construct K Palindrome Strings

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计奇数字符出现次数 日期 题目地址:https:// ...

  7. 【LeetCode】680. Valid Palindrome II 验证回文字符串 Ⅱ(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 思路来源 初版方案 进阶方案 日期 题目地址 ...

  8. 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

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

  9. LeetCode 第五题 最长的回文字符串 (JAVA)

    Longest Palindromic Substring 简介:字符串中最长的回文字符串 回文字符串:中心对称的字符串 ,如 mom,noon 问题详解: 给定一个字符串s,寻找字符串中最长的回文字 ...

随机推荐

  1. [转]Matlab2012b安装详解

    matlab2012b安装文件下载: http://yunpan.cn/cVY5VsSeUXzai (提取码:ec84) 1.双击setup.exe进行安装.安装中选择“不使用Internet安装” ...

  2. redis的事务、主从复制、持久化

    redis事务 和其它数据库一样,Redis作为NoSQL数据库也同样提供了事务机制.在Redis中, MULTI/EXEC/DISCARD/WATCH这四个命令是我们实现事务的基石.Redis中事务 ...

  3. 8.Element-ui日期组件上传到后台日期少一天解决办法

    <el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="转出日期& ...

  4. django-初始配置(纯手写)

    我们通过django-admin startproject zhuyu命令创建好项目后,在pycharm中打开 我们需要在在该项目中,配置一些相关操作. 1.template(存放模板的文件夹) 如果 ...

  5. NEC 框架规范 css function

    /* function */.f-cb:after,.f-cbli li:after{display:block;clear:both;visibility:hidden;height:0;overf ...

  6. Git推送到远程分支出错

    执行git push -u origin master fatal: 'git@github.com:qilinonline/git_test.git' does not appear to be a ...

  7. HTML中的【块】与【内嵌】

    块元素与内嵌元素 块的特征 默认独占一行 没有宽度时默认撑满一行 支持所有的css命令 内嵌的特征 同行可以连续跟同类的标签 内容撑开宽度 不支持宽高 不支持上下的内外边距 代码换行被解析 块与内嵌的 ...

  8. sublime3常用插件总结

    本人之前使用的是webstorm,后来改用sublime,渐渐的爱上了它的快!(自行体会) 正式介绍sublime3常用的一些插件,安装流程不再赘述! SublimeTmpl 创建常用文件初始模板,必 ...

  9. Spark-源码-Spark-Submit 任务提交

    Spark 版本:1.3 调用shell, spark-submit.sh args[] 首先是进入 org.apache.spark.deploy.SparkSubmit 类中调用他的 main() ...

  10. python面向对象-cs游戏示例

    #!/usr/local/bin/python3 # -*- coding:utf-8 -*- class Role(object): n = 123 # 类变量 name = "我是类na ...