原题链接在这里: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.

最新文章

  1. PHP面试题4
  2. Openjudge 1.13-40 提取数字串按数值排序
  3. HashMap合并相同key的value
  4. 深入浅出设计模式——策略模式(Strategy Pattern)
  5. IOS弹出视图 LewPopupViewController
  6. [转]Linux之type命令
  7. 存量数据处理结果查询.txt
  8. 获取电脑cpu的使用情况
  9. html---id,name和value
  10. Python处理海量手机号码
  11. java集合概念
  12. docker~从Dockerfile到Container的过程(终于算是OK了)
  13. 201521123054 《Java程序设计》第14周学习总结
  14. php中一些提高性能的技巧
  15. 阿里巴巴Java开发规约插件
  16. 开源框架Volley的使用《二》[NetWorkImageView&amp;&amp;LruCache&amp;ImageLoader]
  17. 《前端之路》之 Babel 下一代 JavaScript 语法编译器
  18. linux中gcc和g++的区别
  19. Java NIO系列教程(七) selector原理 Epoll版的Selector
  20. Django中的缓存基础知识

热门文章

  1. BScroll使用
  2. idea类存在找不到解决办法
  3. Angular中上传图片到分布式文件服务器FastDFS上
  4. Java基础语法面试题
  5. WPF DataGrid 使用CellTemplateSelector 时SelectTemplate方法Item参数为NULL
  6. 【题解】C2Crni - Crni [COCI2010] [SP7884]
  7. 【05】C#特有的ref、out参数
  8. Cases:Unit Testing with the MSTest Framework
  9. .Net FrameWork获取配置文件信息
  10. 深挖Jvm垃圾收集