Q1: 回文字符串的分割

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

  1. For example, given s = "aab",
  2. Return
  3. [
  4. ["aa","b"],
  5. ["a","a","b"]
  6. ]

算法
回溯法.

  • 从字符串开头扫描, 找到一个下标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"]
  • 然后继续回溯 + 递归.

实现

  1. class Solution {
  2. public:
  3. vector<vector<string>> partition(string s) {
  4. std::vector<std::vector<std::string> > results;
  5. std::vector<std::string> res;
  6. dfs(s, 0, res, results);
  7. return results;
  8. }
  9. private:
  10. void dfs(std::string& s, int startIndex,
  11. std::vector<std::string> res,
  12. std::vector<std::vector<std::string> >& results)
  13. {
  14. if (startIndex >= s.length())
  15. {
  16. results.push_back(res);
  17. }
  18. for (int i = startIndex; i < s.length(); ++i)
  19. {
  20. int l = startIndex;
  21. int r = i;
  22. while (l <= r && s[l] == s[r]) ++l, --r;
  23. if (l >= r)
  24. {
  25. res.push_back(s.substr(startIndex, i - startIndex + 1));
  26. dfs(s, i + 1, res, results);
  27. res.pop_back();
  28. }
  29. }
  30. }
  31. };

  

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.

  1. For example, given s = "aab",
  2. Return 1 since the palindrome partitioning
  3. ["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.

实现

  1. class Solution {
  2.  
  3. public:
  4. int minCut(std::string s) {
  5. int len = s.length();
  6. int minCut = 0;
  7. bool isPalindrome[len][len] = {false};
  8. int dp[len + 1] = {INT32_MAX};
  9. dp[len] = -1;
  10. for (int leftIndex = len - 1; leftIndex >= 0; --leftIndex)
  11. {
  12. for (int midIndex = leftIndex; midIndex <= len - 1; ++midIndex)
  13. {
  14. if ((midIndex - leftIndex < 2 || isPalindrome[leftIndex + 1][midIndex -1])
  15. && s[leftIndex] == s[midIndex])
  16. {
  17. isPalindrome[leftIndex][midIndex] = true;
  18. dp[leftIndex] = std::min(dp[midIndex + 1] + 1, dp[leftIndex]);
  19. }
  20. }
  21. std::cout << leftIndex << ": " << dp[leftIndex] << std::endl;
  22. }
  23. return dp[0];
  24. }
  25. };

  

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. 【luogu P1865 A % B Problem】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1865 其实就是埃拉托色尼筛素数模板... 好像每个数暴力枚举到sqrt()也可以...就算当我无聊练手罢 # ...

  2. Page 由于代码已经过优化或者本机框架位于调用堆栈之上

    Page.Response.Clear();            Page.Response.Write("<script type=\"text/javascript\& ...

  3. 优雅的QSignleton (二) MonoSingleton单例实现

    MonoSingleton.cs namespace QFramework.Example { using System.Collections; using UnityEngine; class C ...

  4. iOS之UIImagePickerController显示中文界面

    iOS开发中,我们经常遇到获取拍照.相册中图片的功能,就必然少不了UIImagePickerController,但是我们发现当我们使用它的时候,它的页面是英文的,看着很别扭,国人还是比较喜欢看中文界 ...

  5. JavaScript 基础(五) 函数 变量和作用域

    函数定义和调用 定义函数,在JavaScript中,定义函数的方式如下: function abs(x){ if(x >=0){ return x; }else{ return -x; } } ...

  6. Angularjs基础(六)

    AngularJS HTML DOM AngularJS为HTML DOM 元素的属性提供了绑定应用数据的指令. ng-disabled指令 ng-disabled指令直接绑定应用数据到HTML的di ...

  7. 表单转换为JSON

    $.fn.serializeObject = function () { var o = {}; var a = this.serializeArray(); $.each(a, function ( ...

  8. WebGL学习笔记(2)

    根据上一篇学习笔记,理解WebGL的工作原理及基本调用代码后,可以开始研究3D顶点对象的缩放.旋转.以及对对象进行可交互(鼠标或键盘控制)的旋转或者缩放. 要理解对象的旋转/缩放需要首先学习矩阵的计算 ...

  9. web常用软件

    编辑器: VSCode HBuilder WebStorm NotePad++ Eclipse Atom 常用插件: SwitchyOmega Vue-Tools server类: tomcat Ng ...

  10. 【PTA 天梯赛】L2-026. 小字辈(广搜+邻接表)

    本题给定一个庞大家族的家谱,要请你给出最小一辈的名单. 输入格式: 输入在第一行给出家族人口总数 N(不超过 100 000 的正整数) —— 简单起见,我们把家族成员从 1 到 N 编号.随后第二行 ...