【leetcode】1278. Palindrome Partitioning III
题目如下:
You are given a string
s
containing lowercase letters and an integerk
. You need to :
- First, change some characters of
s
to other lowercase English letters.- Then divide
s
intok
non-empty disjoint substrings such that each substring is palindrome.Return the minimal number of characters that you need to change to divide the string.
Example 1:
Input: s = "abc", k = 2
Output: 1
Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome.Example 2:
Input: s = "aabbc", k = 3
Output: 0
Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome.Example 3:
Input: s = "leetcode", k = 8
Output: 0Constraints:
1 <= k <= s.length <= 100
.s
only contains lowercase English letters.
解题思路:记dp[i][j] = v 表示s[0:j]区间内分割成j段,只需要改变v个字符就可以使得每一段的子串都是回文串。要求dp[i][j]的值,关键就是找出j和j-1的分割点。很显然有dp[i][j] = min(dp[m][j-1] + s[m:j]需要改变的字符的个数),只需要找出最小的分割点m即可。
代码如下:
class Solution(object):
def palindromePartition(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
def calc(substr):
count = 0
for i in range(len(substr)/2):
if substr[i] != substr[len(substr)-i - 1]:count += 1
return count dp = [[float('inf')] * k for _ in s]
dp[0][0] = 0
for i in range(len(s)):
for j in range(k):
if j == 0:
dp[i][j] = calc(s[:i+1])
continue
for m in range(j-1,i):
dp[i][j] = min(dp[i][j],dp[m][j-1] + calc(s[m+1:i+1]))
#print dp
return dp[-1][-1]
【leetcode】1278. Palindrome Partitioning III的更多相关文章
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】131. Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
- 【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- 【Lintcode】136.Palindrome Partitioning
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- X86逆向13:向程序中插入Dll
本章我们将学习Dll的注入技巧,我们将把一个动态链接库永久的插入到目标程序中,让程序在运行后直接执行这个Dll文件,这一章的内容也可以看作是第八课的加强篇,第八课中我们向程序中插入了一个弹窗,有木有发 ...
- luogu P2423 [HEOI2012]朋友圈 (最大团)
在很久很久以前,曾经有两个国家和睦相处,无忧无虑的生活着. 一年一度的评比大会开始了,作为和平的两国,一个朋友圈数量最多的永远都是最值得他人的尊敬,所以现在就是需要你求朋友圈的最大数目.两个国家看成是 ...
- python之paramiko 远程执行命令
有时会需要在远程的机器上执行一个命令,并获得其返回结果.对于这种情况,python 可以很容易的实现. 1 .工具 Python paramiko 1) Paramiko模块安装 在Linux的Ter ...
- python numpy 的用法——diag函数
当 np.diag(array) 中 array是一个1维数组时,结果形成一个以一维数组为对角线元素的矩阵 array是一个二维矩阵时,结果输出矩阵的对角线元素
- Struts的相关基础
为什么要用struts? 1.该框架基予mvc的开发设计模式上的,所以拥有mvc的全部优点,他在M.V.C上都有涉及,但它主要是提供一个好的控制器和一套定制的标签库上,有mvc的一系列优点:层次分明, ...
- -parameters 参数的使用 解决 Feign PathVariable annotation was empty on param 0
在使用 FeignClient 如果参数没有给默认名字 @PathVariable("districtId") Long districtId 比如 @FeignClient(&q ...
- Gephi简单导入数据
使用工具 Gephi-0.9.2 事前要导入的数据 Node.csv 节点:名称可以所以定,格式.csv Edge.csv 边:名称可以所以定,格式.csv 导入操作 新建项目 导入节点 新建列要与表 ...
- Photoshop从入门到精通所有视频教程(43G)以及素材资料免费拿
包含了Photoshop从入门到精通所有需要了解的视频教程资料,并且包含了大量的P图素材. 资料获取方式,关注公总号RaoRao1994,查看往期精彩-所有文章,即可获取资源下载链接 更多资源获取,请 ...
- python部署到服务器(2) 一一 nginx+uwsgi+Django
参考菜鸟教程,https://blog.csdn.net/qq_42314550/article/details/81805328, 和 https://www.cnblogs.com/chenice ...
- Java 访问 C++ 方法:JavaCPP
JavaCPP提供了在Java中高效访问本地C++的方法.采用JNI技术实现,支持所有Java实现包括Android系统,Avian 和 RoboVM. JavaCPP提供了一系列的Annotatio ...