LeetCode 1216. Valid Palindrome III
原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/
题目:
Given a string s and an integer k, find out if the given string is a K-Palindrome or not.
A string is K-Palindrome if it can be transformed into a palindrome by removing at most k characters from it.
Example 1:
Input: s = "abcdeca", k = 2
Output: true
Explanation: Remove 'b' and 'e' characters.
Constraints:
1 <= s.length <= 1000shas only lowercase English letters.1 <= k <= s.length
题解:
Find the longest palindrome from s.
And check the difference between s and longest palindrome length, if it is <= k, then return true.
Time Complexity: O(n^2). n = s.length().
Space: O(n^2).
AC Java:
class Solution {
public boolean isValidPalindrome(String s, int k) {
if(s == null || s.length() <= k){
return true;
}
int lp = findLongestPalin(s);
return s.length() - lp <= k;
}
private int findLongestPalin(String s){
if(s == null || s.length() == 0){
return 0;
}
int n = s.length();
int [][] dp = new int[n][n];
for(int i = n-1; i>=0; i--){
dp[i][i] = 1;
for(int j = i+1; j<n; j++){
if(s.charAt(i) == s.charAt(j)){
dp[i][j] = dp[i+1][j-1] + 2;
}else{
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
}
}
}
return dp[0][n-1];
}
}
类似Longest Palindromic Subsequence, Valid Palindrome, Valid Palindrome II.
LeetCode 1216. Valid Palindrome III的更多相关文章
- [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][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [leetcode] 1. Valid Palindrome
leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode 125. Valid Palindrome ----- java
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125. Valid Palindrome
这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...
- leetcode:Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- sql 自动增加排序 并且初始值是000001
declare @co_num int, @CoNum varchar(6) select co_num=count(*)+1 from tab ...
- SpringBoot第十三篇:日志处理
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10973583.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 日志是软件 ...
- wifi串口服务器
下面与大家分享上海卓岚无线wifi串口服务器ZLAN7104创建虚拟串口的设置使用心得 一.7104网线连接计算机,用ZLVircom即可搜索并配置 其中,串口设置需要匹配实际所接的串口设备,配置为相 ...
- Linux内核同步机制之原子操作
1.前言 原子操作指的是该操作不会在执行完毕之前被任何其它任务或事件打断,它是最小的执行单位,不会有比它更小的执行单位,原子实际上使用了物理学中物质微粒的概念,在Linux内核中,原子操作需要硬件的支 ...
- Mybatis设置主键自增
<insert id="insertArea" useGeneratedKeys="true" keyProperty="areaId" ...
- session的工作原理与session用法
一直在使用session存储数据,一直没有好好总结一下session的使用方式以及其工作原理,今天在这里做一下梳理. 这里的介绍主要是基于php语言,其他的语言操作可能会有差别,但基本的原理不变. 1 ...
- Sitecore个性化 - 什么是历史个性化?
顾名思义,Sitecore中的历史个性化允许您根据访问者过去在您网站上的行为来设置个性化规则. 许多组织选择Sitecore 作为其高级个性化功能的网站平台 - 历史个性化只是一种方法. 查看我们关 ...
- scala基础题--面向对象1
练习1 编写computer类,包含CPU.内存.硬盘等属性,getDetails方法用于返回computer的详细信息. 编写PC子类,继承computer类,添加特有属性[品牌brand] 编写n ...
- Window应急响应(六):NesMiner挖矿病毒
0x00 前言 作为一个运维工程师,而非一个专业的病毒分析工程师,遇到了比较复杂的病毒怎么办?别怕,虽然对二进制不熟,但是依靠系统运维的经验,我们可以用自己的方式来解决它. 0x01 感染现象 1.向 ...
- kvm虚拟机日常管理与配置
1. 查看KVM虚拟机配置文件及运行状态 (1) KVM虚拟机默认配置文件位置: /etc/libvirt/qemu/ autostart目录是配置kvm虚拟机开机自启动目录. (2) vir ...