【leetcode】1278. Palindrome Partitioning III
题目如下:
You are given a string
scontaining lowercase letters and an integerk. You need to :
- First, change some characters of
sto other lowercase English letters.- Then divide
sintoknon-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.sonly 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 ...
随机推荐
- python+pycharm+PyQt5 图形化界面安装教程
python图形化界面安装教程 配置环境变量 主目录 pip所在目录,及script目录 更新pip(可选) python -m pip install --upgrade pip ps:更新出错一般 ...
- 虚拟机(Vmware)安装ubuntu18.04和配置调整(三)
三.ubuntu安装软件 1.安装常用软件 python程序员: $ sudo apt install ipython $ sudo apt install ipython3 $ sudo a ...
- php修改替换数据库图片(文件)
<?php extract($_POST); $date = date('Y-m-d'); $file_name = $_FILES['image']['name'];//获取缓存区图片,格式不 ...
- STM32的堆与栈与编译信息查看
STM32的堆与栈与编译信息查看 因为一个项目中使用malloc函数动态分配内存400多个字节,返回为0,分配失败.查找失败原因,为堆空间不足分配导致.查看堆和栈分别设置了2K,按正常情况看应能满足分 ...
- JavaScript例子2-使一个特定的表格隔行变色
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Ubuntu12.04+Caffe (+OpenCV+CPU-only)
经过一天的努力发现12.04 的pcre的库太低了, 要解决这个bug只能升级系统到16.04 麻蛋!!! 1. 下载大神MTCNN 源码,内含caffe https://github.co ...
- JSTreeShaking的webpack-deep-scope-plugin插件的应用
webpack自身实现词法分析的JSTreeShaking webpack-depp-scope-plugin插件实现作用域分析的JSTreeShaking 一.webpack词法分析的JSTreeS ...
- php--常见算法1
<?php //递归输出123321 function digui($num){ echo $num; if($num<3){ digui($num+1); } echo $num; } ...
- java_day07_异常
第七章: 异常 1.异常概述 在我们日常生活中,有时会出现各种各样的异常,例如:职工小王开车去上班,在正常情况下,小王会准时到达单位.但是天有不测风云,在小王去上班时,可能会遇到一些异常情况,比如小王 ...
- Delphi TIdUDPClient组件