在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a string A string is K-Palindrome if it can be transformed into a palindrome by removing at most Example 1: Input: s = "abcdeca", k = 2 Output: true Explanation: Remove 'b' and 'e' characters. Constraints:
给出一个字符串 所谓「K 回文」:如果可以通过从字符串中删去最多 示例: 输入:s = "abcdeca", k = 2 输出:true 解释:删除字符 “b” 和 “e”。 提示:
Runtime: 112 ms
Memory Usage: 21.3 MB
1 class Solution { 2 func isValidPalindrome(_ s: String, _ k: Int) -> Bool { 3 let n:Int = s.count 4 var s:[Character] = Array(s) 5 var dp:[Int] = [Int](repeating: 1, count: n) 6 for i in 0..<n 7 { 8 var last:Int = 0 9 for j in stride(from: i - 1, through: 0, by: -1) 10 { 11 var t:Int = dp[j] 12 if s[i] == s[j] 13 { 14 dp[j] = last + 2 15 } 16 else 17 { 18 dp[j] = max(dp[j], dp[j + 1]) 19 } 20 last = t 21 } 22 } 23 return n - dp[0] <= k 24 } 25 }
|
请发表评论