Valid Palindrome III
The problem
Given a string s and an integer k, return true if s is a k-palindrome.
A string is k-palindrome if it can be transformed into a palindrome by removing at most k characters from it.
Input: s = "abcdeca", k = 2 Output: true Explanation: Remove 'b' and 'e' characters.
Input: s = "abbababa", k = 1 Output: true
Input s = "bacabaaa", k =2
1 <= s.length <= 1000 s consists of only lowercase English letters. 1 <= k <= s.length
cpp
class Solution {
public:
bool isValidPalindrome(string s, int k) {
//User code goes here
}
};java
class Solution {
public boolean isValidPalindrome(String s, int k) {
// User code goes here
}
}python
class Solution:
def isValidPalindrome(self, s: str, k: int) -> bool:
# User code goes here
return Falsejavascript
class Solution {
isValidPalindrome(s, k) {
// User code goes here
}
}csharp
public class Solution
{
public bool IsValidPalindrome(string s, int k)
{
// User code goes here
return false;
}
}go
func isValidPalindrome(s string, k int) bool {
// User code goes here
return false
}Stuck? Show a way to structure it+
- 01Compute the minimum deletions needed to make the string a palindrome, or equivalently its longest palindromic subsequence.
- 02Use matching ends directly; otherwise discard one end and take the better state.
- 03Return whether the minimum deletion count is at most k.
- 04Memoize or fill bottom-up so overlapping intervals are solved once.
Reference answer
Then expect these follow-ups
Why is minimum deletion count n minus LPS?
Tests: equivalence proof
How would you construct the resulting palindrome?
Tests: reconstruction
Free to read · better with Enzo
Practice this out loud with Enzo
Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.
Next question