Talentd
FleetCode

1216. Valid Palindrome III

Hard49.2% acceptance3 companiesPremium elsewhere, free here

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 1:

Input: s = "abcdeca", k = 2
Output: true
Explanation: Remove 'b' and 'e' characters.

Example 2:

Input: s = "abbababa", k = 1
Output: true

Constraints:

  • 1 <= s.length <= 1000
  • s consists of only lowercase English letters.
  • 1 <= k <= s.length

Asked at 3 companies

Hints open one at a time

Hint 1
Can you reduce this problem to a classic problem?
Hint 2
The problem is equivalent to finding any palindromic subsequence of length at least N-K where N is the length of the string.
Hint 3
Try to find the longest palindromic subsequence.
Hint 4
Use DP to do that.
16:01
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
"abcdeca"
2
Input
"abbababa"
1
Your code is saved on this device.Solution