1531. String Compression II

Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string "aabccc" we replace "aa" by "a2" and replace "ccc" by "c3". Thus the compressed string becomes "a2bc3".

Notice that in this problem, we are not adding '1' after single characters.

Given a string s and an integer k. You need to delete at most k characters from s such that the run-length encoded version of s has minimum length.

Find the minimum length of the run-length encoded version of s after deleting at most k characters.

 

Example 1:

Input: s = "aaabcccd", k = 2
Output: 4
Explanation: Compressing s without deleting anything will give us "a3bc3d" of length 6. Deleting any of the characters 'a' or 'c' would at most decrease the length of the compressed string to 5, for instance delete 2 'a' then we will have s = "abcccd" which compressed is abc3d. Therefore, the optimal way is to delete 'b' and 'd', then the compressed version of s will be "a3c3" of length 4.

Example 2:

Input: s = "aabbaa", k = 2
Output: 2
Explanation: If we delete both 'b' characters, the resulting compressed string would be "a4" of length 2.

Example 3:

Input: s = "aaaaaaaaaaa", k = 0
Output: 3
Explanation: Since k is zero, we cannot delete anything. The compressed string is "a11" of length 3.

 

Constraints:

  • 1 <= s.length <= 100
  • 0 <= k <= s.length
  • s contains only lowercase English letters.

Rust Solution

struct Solution;

use std::collections::HashMap;

impl Solution {
    fn get_length_of_optimal_compression(s: String, k: i32) -> i32 {
        let mut memo: HashMap<(usize, usize, char, usize), usize> = HashMap::new();
        let n = s.len();
        let k = k as usize;
        let s: Vec<char> = s.chars().collect();

        Self::dp(0, k, ' ', 0, &mut memo, &s, n) as i32
    }

    fn compressed_length(len: usize) -> usize {
        if len <= 1 {
            len
        } else if len < 10 {
            2
        } else if len < 100 {
            3
        } else {
            4
        }
    }

    fn dp(
        start: usize,
        k: usize,
        c: char,
        m: usize,
        memo: &mut HashMap<(usize, usize, char, usize), usize>,
        s: &[char],
        n: usize,
    ) -> usize {
        if let Some(&res) = memo.get(&(start, k, c, m)) {
            res
        } else {
            let res = if start == n {
                Self::compressed_length(m)
            } else {
                let mut res = std::usize::MAX;
                if k > 0 {
                    res = res.min(Self::dp(start + 1, k - 1, c, m, memo, s, n));
                }
                if s[start] == c {
                    res = res.min(Self::dp(start + 1, k, c, m + 1, memo, s, n));
                } else {
                    res = res.min(
                        Self::compressed_length(m)
                            + Self::dp(start + 1, k, s[start], 1, memo, s, n),
                    );
                }
                res
            };
            memo.insert((start, k, c, m), res);
            res
        }
    }
}

#[test]
fn test() {
    let s = "aaabcccd".to_string();
    let k = 2;
    let res = 4;
    assert_eq!(Solution::get_length_of_optimal_compression(s, k), res);
    let s = "aabbaa".to_string();
    let k = 2;
    let res = 2;
    assert_eq!(Solution::get_length_of_optimal_compression(s, k), res);
    let s = "aaaaaaaaaaa".to_string();
    let k = 0;
    let res = 3;
    assert_eq!(Solution::get_length_of_optimal_compression(s, k), res);
}

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