1216 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:
Constraints:
1 <= s.length <= 1000
s
has only lowercase English letters.1 <= k <= s.length
这题一开始的想法是运用680 Valid Palindrome II 递归的方法继续下去。譬如,我们只能去掉左边,去掉右边,或者两边都去掉。最后如果3个之中有一个可行的,就return true。但倒了47/50的地方,有一个test case过不了...还以为自己神功附体,突然写出来了hard题,而且还是memorization的写...是我想多了...(在leetCodeJan-string里)
正确的打开方式是,用DP。跟72 Edit Distance和516 Longest Palindromic Subsequence 相似。其实我的解法,差了一步,就是去掉左or右的时候,没有取最小值。
Last updated
Was this helpful?