3163. String Compression III Medium
1/**
2 * [3163] String Compression III
3 *
4 * Given a string word, compress it using the following algorithm:
5 *
6 * Begin with an empty string comp. While word is not empty, use the following operation:
7 *
8 * Remove a maximum length prefix of word made of a single character c repeating at most 9 times.
9 * Append the length of the prefix followed by c to comp.
10 *
11 *
12 *
13 * Return the string comp.
14 *
15 * <strong class="example">Example 1:
16 * <div class="example-block">
17 * Input: <span class="example-io">word = "abcde"</span>
18 * Output: <span class="example-io">"1a1b1c1d1e"</span>
19 * Explanation:
20 * Initially, comp = "". Apply the operation 5 times, choosing "a", "b", "c", "d", and "e" as the prefix in each operation.
21 * For each prefix, append "1" followed by the character to comp.
22 * </div>
23 * <strong class="example">Example 2:
24 * <div class="example-block">
25 * Input: <span class="example-io">word = "aaaaaaaaaaaaaabb"</span>
26 * Output: <span class="example-io">"9a5a2b"</span>
27 * Explanation:
28 * Initially, comp = "". Apply the operation 3 times, choosing "aaaaaaaaa", "aaaaa", and "bb" as the prefix in each operation.
29 *
30 * For prefix "aaaaaaaaa", append "9" followed by "a" to comp.
31 * For prefix "aaaaa", append "5" followed by "a" to comp.
32 * For prefix "bb", append "2" followed by "b" to comp.
33 * </div>
34 *
35 * Constraints:
36 *
37 * 1 <= word.length <= 2 * 10^5
38 * word consists only of lowercase English letters.
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/string-compression-iii/
44// discuss: https://leetcode.com/problems/string-compression-iii/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn compressed_string(word: String) -> String {
50 String::new()
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_3163() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.