题目如下:

You are given a string s containing lowercase letters and an integer k. You need to :

  • First, change some characters of s to other lowercase English letters.
  • Then divide s into k 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: 0

Constraints:

  • 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的更多相关文章

  1. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  2. 【LeetCode】132. Palindrome Partitioning II

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

  3. 【LeetCode】131. Palindrome Partitioning

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

  4. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  5. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  6. 【Lintcode】136.Palindrome Partitioning

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  7. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  8. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  9. 【LeetCode】234. Palindrome Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. NOIP模拟赛 打铁的匠 题解

    [问题描述] Mark Douglas是一名优秀的锻造师.与他优秀的锻造水平不相符,他非常穷,以至于很多好刀都因为缺少素材缺少资金无法打造. Mark把他有能力锻造的所有n种刀建成了一棵锻造树,除了第 ...

  2. gin框架博客实战教程2019web页面开发go语言实战博客开发

    视频教程: https://www.bilibili.com/video/av73698322?t=2400&p=5 资料下载地址(含数据库和main.go和controller里的代码) 注 ...

  3. laravel-admin关联查询问题解决办法

    文档是这么说的: 按照文档上来,没有成功,网上找了好久,说是没有在模型中关联,关联之后的运行结果是这样的: 还是没有成功啊,仔细研究返现是这里写错了,whereHas后面跟的是model中的方法名,而 ...

  4. Python三大主流框架的对比

    相信做Python这一块的程序员都有听说这三个框架,就像神一样的存在,每一个框架的介绍我就不写出来了,感兴趣可以自己百度了解了解!下面我就说正事 Django:Python 界最全能的 web 开发框 ...

  5. 如何给django admin.py配置超级管理员?注册表格?

    admin.py是django给我们提供的功能非常强大的后台,况且支持拓展,,如果你要是觉得admin的后台不够牛逼你可以自己写一个!如何自己写一个后台,后面我有时间了会给大家更新!一起学习!一起进步 ...

  6. 深入理解计算机系统 第十章 系统级I/O 第二遍

    了解 Unix I/O 的好处 了解 Unix I/O 将帮助我们理解其他的系统概念 I/O 是系统操作不可或缺的一部分,因此,我们经常遇到 I/O 和其他系统概念之间的循环依赖.例如,I/O 在进程 ...

  7. NET Core:搭建私有Nuget服务器以及打包发布Nuget包

    docker 安装 https://www.cnblogs.com/liuxiaoji/p/11014329.html 1.使用docker搭建私有Nuget服务器 docker run -d -p ...

  8. EasyUI_前台js_省市县三级联动

    1.html: <td class="tdl">所属城市</td> <td class="td_detail"> <i ...

  9. C#委托的定义 以及使用方式详解,更简单的理解委托。

    委托的声明及定义: 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得 ...

  10. 【原创】大叔问题定位分享(35)spring中session失效时间

    spring项目中将sessionid对应的cookie过期时间设置很长,但是实际session还是在半个小时后失效,跟了一下代码,spring中session实现接口为 org.springfram ...