Skip to content

Commit a626f79

Browse files

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1155,3 +1155,4 @@ mod s1526_minimum_number_of_increments_on_subarrays_to_form_a_target_array;
11551155
mod s1528_shuffle_string;
11561156
mod s1529_minimum_suffix_flips;
11571157
mod s1530_number_of_good_leaf_nodes_pairs;
1158+
mod s1531_string_compression_ii;
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* [1531] String Compression II
3+
*
4+
* <a href="http://en.wikipedia.org/wiki/Run-length_encoding">Run-length encoding</a> 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 <font face="monospace">"aa"</font> by <font face="monospace">"a2"</font> and replace <font face="monospace">"ccc"</font> by <font face="monospace">"c3"</font>. Thus the compressed string becomes <font face="monospace">"a2bc3".</font>
5+
* Notice that in this problem, we are not adding '1' after single characters.
6+
* 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.
7+
* Find the minimum length of the run-length encoded version of s after deleting at most k characters.
8+
*
9+
* Example 1:
10+
*
11+
* Input: s = "aaabcccd", k = 2
12+
* Output: 4
13+
* 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.
14+
* Example 2:
15+
*
16+
* Input: s = "aabbaa", k = 2
17+
* Output: 2
18+
* Explanation: If we delete both 'b' characters, the resulting compressed string would be "a4" of length 2.
19+
*
20+
* Example 3:
21+
*
22+
* Input: s = "aaaaaaaaaaa", k = 0
23+
* Output: 3
24+
* Explanation: Since k is zero, we cannot delete anything. The compressed string is "a11" of length 3.
25+
*
26+
*
27+
* Constraints:
28+
*
29+
* 1 <= s.length <= 100
30+
* 0 <= k <= s.length
31+
* s contains only lowercase English letters.
32+
*
33+
*/
34+
pub struct Solution {}
35+
36+
// problem: https://leetcode.com/problems/string-compression-ii/
37+
// discuss: https://leetcode.com/problems/string-compression-ii/discuss/?currentPage=1&orderBy=most_votes&query=
38+
39+
// submission codes start here
40+
41+
impl Solution {
42+
// Credit: https://leetcode.com/problems/string-compression-ii/solutions/2709343/rust-bottom-up-dp-100-time-and-memory-25-lines-of-idiomatic-code-commented-version/
43+
pub fn get_length_of_optimal_compression(s: String, k: i32) -> i32 {
44+
let mut dp = [[i32::MAX - 101; 101]; 101];
45+
46+
dp[0].fill(0);
47+
48+
for (i, c) in s.bytes().enumerate().map(|(i, c)| (i + 1, c)) {
49+
for to_rem in 0..=(i).min(k as usize) {
50+
if to_rem > 0 {
51+
dp[i][to_rem] = dp[i - 1][to_rem - 1];
52+
}
53+
54+
let mut count = 0_i32;
55+
let mut removed = 0;
56+
for (j, c_) in s.bytes().enumerate().take(i).rev() {
57+
if c_ == c {
58+
count += 1;
59+
} else {
60+
removed += 1;
61+
if removed > to_rem {
62+
break;
63+
}
64+
}
65+
let f = |c: i32| (c as f32).log10().floor() as i32 + 1 + (c != 1) as i32;
66+
dp[i][to_rem] = dp[i][to_rem].min(dp[j][to_rem - removed] + f(count));
67+
}
68+
}
69+
}
70+
dp[s.len()][k as usize]
71+
}
72+
}
73+
74+
// submission codes end
75+
76+
#[cfg(test)]
77+
mod tests {
78+
use super::*;
79+
80+
#[test]
81+
fn test_1531_example_1() {
82+
let s = "aaabcccd".to_string();
83+
let k = 2;
84+
85+
let result = 4;
86+
87+
assert_eq!(Solution::get_length_of_optimal_compression(s, k), result);
88+
}
89+
90+
#[test]
91+
fn test_1531_example_2() {
92+
let s = "aabbaa".to_string();
93+
let k = 2;
94+
95+
let result = 2;
96+
97+
assert_eq!(Solution::get_length_of_optimal_compression(s, k), result);
98+
}
99+
100+
#[test]
101+
fn test_1531_example_3() {
102+
let s = "aaaaaaaaaaa".to_string();
103+
let k = 0;
104+
105+
let result = 3;
106+
107+
assert_eq!(Solution::get_length_of_optimal_compression(s, k), result);
108+
}
109+
}

0 commit comments

Comments
 (0)