leetcode-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:
1 | Input: s = "abcdeca", k = 2 |
Constraints:
1 <= s.length <= 1000
s
has only lowercase English letters.1 <= k <= s.length
分析
思路1: 使用动态规划,$dp[i][j]$ 表示字符串 s[i...j]
转化为回文最小需要删除多少个字符。则最终答案,return dp[0][n-1] <= k
。
思路2: 求 s
的最长回文子序列,然后 s
的长度减去其最长回文子序列的长度,如果小于等于 k
则结果为 true
,否则结果为 false
。
代码1
1 | // 思路1,自顶向下 |
代码2
1 | // 思路1,自底向上 |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/04/02/leetcode-1216/
License: 知识共享署名-非商业性使用 4.0 国际许可协议