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,寻找字符串中最长的回文字 ...
随机推荐
- axis调用cxf的webservice注意事项
需要注意的是: 1.wsdl显示部分内容 <?xml version="1.0" ?> - <wsdl:definitions name="Archiv ...
- HDU 1110 Equipment Box (判断一个大矩形里面能不能放小矩形)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1110 Equipment Box Time Limit: 2000/1000 MS (Java/Oth ...
- Python—面向对象01
1.如何使用类 # 先定义类 class LuffyStudent(): school = "luffycity" # 数据属性 def learn(self): # 函数属性 p ...
- Mac 使用问题
Mac 使用 Mac改变系统截图存储位置 鼠标在屏幕中间上下滚动时,有时反应不灵敏: 看看偏好设置-> 鼠标 -> 跟踪速度或者手势部分是否有哪里设置不当: 考虑重新开启鼠标鼠标底部有一个 ...
- ccenteros 部署 redis
step one : yum install redis -- 安装redis数据库 step two:安装完成之后开启redis 服务 service redis start syste ...
- nodejs+express开发blog(1)
前言 Nodejs是一个年轻的编程框架,充满了活力和无限激情,一直都在保持着快速更新.基于Nodejs的官方Web开发库Express也在同步发展着,每年升级一个大版本,甚至对框架底层都做了大手术.在 ...
- sudo及visudo
sudo是一种权限管理机制,管理员可以授权普通用户去执行root的操作,而不需要知道root的密码,它依赖于/etc/sudoers这个文件,可以授权给哪个用户在哪个主机上能够以管理员的身份执行什么样 ...
- keepalived入门
简介 Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服 ...
- 密钥登录LINUX步骤
1.创建目录2.创建一个文件3.给目录和文件授权4.关闭防火墙5.然后才可以登录.
- Java源码解析——集合框架(四)——LinkedListLinkedList原码分析
LinkedList源码分析 LinkedList也和ArrayList一样实现了List接口,但是它执行插入和删除操作时比ArrayList更加高效,因为它是基于链表的.基于链表也决定了它在随机访问 ...