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 ...
随机推荐
- MAC版本的UltraEdit破解方法
MAC版本的UltraEdit破解方法: 解压,然后在命令行里输入 printf '\x31\xC0\xFF\xC0\xC3\x90' | dd seek=$((0x777160)) conv=not ...
- zipfile
zipfile是一个用于处理zip压缩格式的文件的模块, 主要会用到它的ZipFile类 import zipfile zipfile.is_zipfile('myzip.zip')) # 判断一个文 ...
- pom中更换阿里云仓库时不要忽略了pluginRepositories
用maven也大几年了,也一直在用阿里云的中央仓库. 不喜欢在maven的settings.xml里改,更喜欢直接在pom.xml里改,因为受git管理,小伙伴们拉下来即可. 然而网上的大部分技术文章 ...
- Git基础-第2章
简单的Git基础概念: repository: 仓库 track: 跟踪 stage: 暂存 commit: 提交 push: 推送 pull: 拉取 一.获取Git仓库 ...
- PHP 命名空间笔记
PHP 命名空间笔记 1.php文件代码如下<pre><?php//我用这样的命名空间表示处于blog下的article模块namespace Blog\Article; class ...
- Web支持HTTPS的client(HTTP&XML-RPC)
生成Web自签名的证书(在命令行执行以下命令) keytool -genkey -keysize 2048 -validity 3650 -keyalg RSA -dname "CN=Han ...
- digital clock based C
/********************************** * Name : timeDisplay.cpp * Purpose: Display digital clock accord ...
- 针对接口编程能帮助达到面向对象开发和设计中"低耦合"的要求. 某公司...打印机...(笔试中遇到的题目)
针对接口编程能帮助达到面向对象开发和设计中"低耦合"的要求. 举个例子:某公司有一台特殊打印机,还可以使用一年,一年后可能换为另一种打印机,这两种打印机都特殊而贵. ...
- Dapper学习(三)之其他用法
这里说的其他用法,是指 Async,Buffered,Transaction,Stored Procedure. 1. 首先 dapper支持异步 ExecuteAsync, QueryAsync, ...
- asp.net core 在centeros 7.x下创建服务
Netcore服务生成说明 如有个项目/opt/wwwroot/dpms.1633.com 启动为/usr/bin/dotnet /opt/wwwroot/dpms.1633.com/DPMS.Web ...