1312. Minimum Insertion Steps to Make a String Palindrome

Given a string s. In one step you can insert any character at any index of the string.

Return the minimum number of steps to make s palindrome.

Palindrome String is one that reads the same backward as well as forward.

 

Example 1:

Input: s = "zzazz"
Output: 0
Explanation: The string "zzazz" is already palindrome we don't need any insertions.

Example 2:

Input: s = "mbadm"
Output: 2
Explanation: String can be "mbdadbm" or "mdbabdm".

Example 3:

Input: s = "leetcode"
Output: 5
Explanation: Inserting 5 characters the string becomes "leetcodocteel".

Example 4:

Input: s = "g"
Output: 0

Example 5:

Input: s = "no"
Output: 1

 

Constraints:

  • 1 <= s.length <= 500
  • All characters of s are lower case English letters.

Rust Solution

struct Solution;

use std::collections::HashMap;

impl Solution {
    fn min_insertions(s: String) -> i32 {
        let n = s.len();
        let s: Vec<char> = s.chars().collect();
        let mut memo: HashMap<(usize, usize), i32> = HashMap::new();
        Self::dp(0, n, &mut memo, &s)
    }
    fn dp(start: usize, end: usize, memo: &mut HashMap<(usize, usize), i32>, s: &[char]) -> i32 {
        let n = end - start;
        if n < 2 {
            return 0;
        }
        if let Some(&res) = memo.get(&(start, end)) {
            return res;
        }
        let res = if s[start] == s[end - 1] {
            Self::dp(start + 1, end - 1, memo, s)
        } else {
            1 + Self::dp(start, end - 1, memo, s).min(Self::dp(start + 1, end, memo, s))
        };
        memo.insert((start, end), res);
        res
    }
}

#[test]
fn test() {
    let s = "zzazz".to_string();
    let res = 0;
    assert_eq!(Solution::min_insertions(s), res);
    let s = "mbadm".to_string();
    let res = 2;
    assert_eq!(Solution::min_insertions(s), res);
    let s = "leetcode".to_string();
    let res = 5;
    assert_eq!(Solution::min_insertions(s), res);
    let s = "g".to_string();
    let res = 0;
    assert_eq!(Solution::min_insertions(s), res);
    let s = "no".to_string();
    let res = 1;
    assert_eq!(Solution::min_insertions(s), res);
}

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