Leetcode. 回文字符串的分割和最少分割数
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. 回文字符串的分割和最少分割数的更多相关文章
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [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 ...
- LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1
680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...
- 【LeetCode】1400. 构造 K 个回文字符串 Construct K Palindrome Strings
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计奇数字符出现次数 日期 题目地址:https:// ...
- 【LeetCode】680. Valid Palindrome II 验证回文字符串 Ⅱ(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 思路来源 初版方案 进阶方案 日期 题目地址 ...
- 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串
131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...
- LeetCode 第五题 最长的回文字符串 (JAVA)
Longest Palindromic Substring 简介:字符串中最长的回文字符串 回文字符串:中心对称的字符串 ,如 mom,noon 问题详解: 给定一个字符串s,寻找字符串中最长的回文字 ...
随机推荐
- 【luogu P1865 A % B Problem】 题解
题目链接:https://www.luogu.org/problemnew/show/P1865 其实就是埃拉托色尼筛素数模板... 好像每个数暴力枚举到sqrt()也可以...就算当我无聊练手罢 # ...
- Page 由于代码已经过优化或者本机框架位于调用堆栈之上
Page.Response.Clear(); Page.Response.Write("<script type=\"text/javascript\& ...
- 优雅的QSignleton (二) MonoSingleton单例实现
MonoSingleton.cs namespace QFramework.Example { using System.Collections; using UnityEngine; class C ...
- iOS之UIImagePickerController显示中文界面
iOS开发中,我们经常遇到获取拍照.相册中图片的功能,就必然少不了UIImagePickerController,但是我们发现当我们使用它的时候,它的页面是英文的,看着很别扭,国人还是比较喜欢看中文界 ...
- JavaScript 基础(五) 函数 变量和作用域
函数定义和调用 定义函数,在JavaScript中,定义函数的方式如下: function abs(x){ if(x >=0){ return x; }else{ return -x; } } ...
- Angularjs基础(六)
AngularJS HTML DOM AngularJS为HTML DOM 元素的属性提供了绑定应用数据的指令. ng-disabled指令 ng-disabled指令直接绑定应用数据到HTML的di ...
- 表单转换为JSON
$.fn.serializeObject = function () { var o = {}; var a = this.serializeArray(); $.each(a, function ( ...
- WebGL学习笔记(2)
根据上一篇学习笔记,理解WebGL的工作原理及基本调用代码后,可以开始研究3D顶点对象的缩放.旋转.以及对对象进行可交互(鼠标或键盘控制)的旋转或者缩放. 要理解对象的旋转/缩放需要首先学习矩阵的计算 ...
- web常用软件
编辑器: VSCode HBuilder WebStorm NotePad++ Eclipse Atom 常用插件: SwitchyOmega Vue-Tools server类: tomcat Ng ...
- 【PTA 天梯赛】L2-026. 小字辈(广搜+邻接表)
本题给定一个庞大家族的家谱,要请你给出最小一辈的名单. 输入格式: 输入在第一行给出家族人口总数 N(不超过 100 000 的正整数) —— 简单起见,我们把家族成员从 1 到 N 编号.随后第二行 ...