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
Hiring now
Adobe has 203 open roles right now
landed applies to roles like these for you, on each company’s own site, with answers written for the role.
2027 Intern - Solutions Consulting
Paris
2027 Intern - Communications
San Francisco
Adobe Intern FY27
Bangalore
Next question