Valid Palindrome III - 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.
Example:
For string "abcdeca" and k = 2, we can remove characters at positions 1 and 4 ('b' and 'e') to get "acdca", which can be rearranged to form palindrome "aceca". However, this problem asks if we can form a palindrome by removing characters while maintaining their relative order.
Input & Output
Example 1 — Basic k-palindrome
$
Input:
s = "abcdeca", k = 2
›
Output:
true
💡 Note:
We can remove 'b' and 'e' to get "acdca", then rearrange to "aceca" (palindrome). Actually, by just removing 'b' and 'e' while keeping order, we get "acdca" which needs 2 more deletions to become "aca" (palindrome). Total = 2 ≤ k.
Example 2 — Already palindrome
$
Input:
s = "abcba", k = 1
›
Output:
true
💡 Note:
String is already a palindrome, needs 0 deletions which is ≤ k=1.
Example 3 — Not enough deletions
$
Input:
s = "abc", k = 1
›
Output:
false
💡 Note:
To make "abc" a palindrome, we need to delete at least 2 characters (e.g., delete 'b' and 'c' to get "a"). Since 2 > k=1, return false.
Constraints
- 1 ≤ s.length ≤ 1000
- 0 ≤ k ≤ s.length
- s consists only of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Given string "abcdeca" and k=2 deletions allowed
2
Find Min Deletions
Calculate minimum deletions needed using DP
3
Compare with k
Check if min deletions ≤ k
Key Takeaway
🎯 Key Insight: Use DP to find minimum deletions needed, compare with k limit
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code