1216. Valid Palindrome III

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

Rust Solution

struct Solution;

use std::collections::HashMap;

impl Solution {
    fn is_valid_palindrome(s: String, k: i32) -> bool {
        let mut memo: HashMap<&str, usize> = HashMap::new();
        let k = k as usize;
        Self::dp(&s, &mut memo) <= k
    }

    fn dp<'a>(s: &'a str, memo: &mut HashMap<&'a str, usize>) -> usize {
        if s.len() < 2 {
            return 0;
        }
        if let Some(&res) = memo.get(&s) {
            return res;
        }
        let n = s.len();
        let res = if s.chars().next().unwrap() == s.chars().next_back().unwrap() {
            Self::dp(&s[1..n - 1], memo)
        } else {
            Self::dp(&s[0..n - 1], memo).min(Self::dp(&s[1..n], memo)) + 1
        };
        memo.insert(s, res);
        res
    }
}

#[test]
fn test() {
    let s = "abcdeca".to_string();
    let k = 2;
    let res = true;
    assert_eq!(Solution::is_valid_palindrome(s, k), res);
}

Having problems with this solution? Click here to submit an issue on github.