原题链接在这里: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 <= 1000
  • s has 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 SubsequenceValid PalindromeValid Palindrome II.

LeetCode 1216. Valid Palindrome III的更多相关文章

  1. [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 ...

  2. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. [leetcode] 1. Valid Palindrome

    leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...

  4. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  6. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. leetcode 125. Valid Palindrome ----- java

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. LeetCode 125. Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

  9. leetcode:Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. Nginx开启gzip提高页面加载速度

    今天发现页面多次请求数据且加载的数据大,需要等待的时间很长 记得Nginx有gzip,可以对数据进行压缩 gzip是nginx服务器的ngx_http_gzip_module模块提供的在线实时数据压缩 ...

  2. k8s-jenkins x CI/CD 动态创建slave---01

    jenkins CI/CD(动态创建slave)简述: 由于之前管理kubernetes集群应用发布,用的是Gitlab-CI,用作开发环境管理还可以,生产环境管理发布,缺点太多,打包速度很慢.研究新 ...

  3. Python 中 如何将两个列表元素一一对应成字典

    使用python的 zip函数 和强大的集合操作可以方便的将两个list元素一一对应转换为dict,如下示例代码: names = ['n1','n2','n3'] values = [1,2,3] ...

  4. 前端开发vscode必备插件

    VSCode 插件 Atom one Dark Theme Atom Dark主题 Auto Close Tag 自动关闭标签 Auto Rename Tag 自动重命名标签 Beautify 格式化 ...

  5. url拼接参数格式

    在一些情况下,需要直接往url上拼接请求参数. http://www.yanggb.com?flag=1&type=normal&role=customer 通过上面的例子就可以看出, ...

  6. Appium+python自动化(八)- 初识琵琶女Appium(千呼万唤始出来,犹抱琵琶半遮面)- 下(超详解)

    ​简介 通过上一篇宏哥给各位小伙伴们的引荐,大家移动对这位美女有了深刻的认识,而且她那高超的技艺和婀娜的身姿久久地浮现在你的脑海里,是不是这样呢???不要害羞直接告诉宏哥:是,就对了.宏哥要的就是这个 ...

  7. 云原生生态周报 Vol. 16 | CNCF 归档 rkt,容器运行时“上古”之战老兵凋零

    作者列表:木苏,临石,得为,等等 业界要闻 安全漏洞 CVE-2019-9512 CVE-2019-9514 http2 的 DOS 漏洞,一旦攻击成功会耗尽服务器的 cpu/mem,从而导致服务不可 ...

  8. Mysql系列(十)—— 性能分析工具profiling

    转载自:http://www.ywnds.com/?p=8677 explain是从mysql怎样解析执行sql的角度分析sql优劣.profiling是从sql执行时资源使用情况的角度来分析sql. ...

  9. ECS上搭建Docker(CentOS7)

    ECS上搭建Docker(CentOS7) centos下 yum快速安装maven ## 安装jdk8 yum install -y java-1.8.0-openjdk* ## 安装maven w ...

  10. 轻量级ORM《sqlcommon》第一个版本发布了!!!

    一.sqlcommon的特色 1. 轻量级,整个包只有123kb. 2. 性能好,自测... 3. API和功能简单.代码简短.可维护性好基本都能看懂.这个点我认为很重要,你不用为了实现一个需求而四处 ...