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 <= 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:

 1 class Solution {
 2     public boolean isValidPalindrome(String s, int k) {
 3         if(s == null || s.length() <= k){
 4             return true;
 5         }
 6
 7         int lp = findLongestPalin(s);
 8         return s.length() - lp <= k;
 9     }
10
11     private int findLongestPalin(String s){
12         if(s == null || s.length() == 0){
13             return 0;
14         }
15
16         int n = s.length();
17         int [][] dp = new int[n][n];
18         for(int i = n-1; i>=0; i--){
19             dp[i][i] = 1;
20             for(int j = i+1; j<n; j++){
21                 if(s.charAt(i) == s.charAt(j)){
22                     dp[i][j] = dp[i+1][j-1] + 2;
23                 }else{
24                     dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
25                 }
26             }
27         }
28
29         return dp[0][n-1];
30     }
31 }

类似Longest Palindromic SubsequenceValid PalindromeValid Palindrome II.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12028253.html

时间: 2024-08-01 14:12:33

LeetCode 1216. Valid Palindrome III的相关文章

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

【Leetcode】Valid Palindrome

题目链接:https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a ca

[leetcode] 1. Valid Palindrome

leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider that the string might be empty? This is a good question to ask during an interview. For

[LeetCode][JavaScript]Valid Palindrome

https://leetcode.com/problems/valid-palindrome/ Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."rac

leetCode 125. Valid Palindrome 字符串

125. Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Hav

[Leetcode][JAVA] Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th

[LeetCode] 125. Valid Palindrome 有效回文

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th

Java [Leetcode 125]Valid Palindrome

题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider t

【LeetCode】- Valid Palindrome(正确的回文)

[ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. 直译:给你一个字符串, 判定它是否是回文(只统计字母.数字,其他字符请忽略). For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" i