2716. Minimize String Length Easy
1/**
2 * [2716] Minimize String Length
3 *
4 * Given a string s, you have two types of operation:
5 * <ol>
6 * Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if exists).
7 * Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the right of i (if exists).
8 * </ol>
9 * Your task is to minimize the length of s by performing the above operations zero or more times.
10 * Return an integer denoting the length of the minimized string.
11 *
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">s = "aaabc"</span>
15 * Output: <span class="example-io">3</span>
16 * Explanation:
17 * <ol>
18 * Operation 2: we choose i = 1 so c is 'a', then we remove s[2] as it is closest 'a' character to the right of s[1].<br />
19 * s becomes "aabc" after this.
20 * Operation 1: we choose i = 1 so c is 'a', then we remove s[0] as it is closest 'a' character to the left of s[1].<br />
21 * s becomes "abc" after this.
22 * </ol>
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">s = "cbbd"</span>
27 * Output: <span class="example-io">3</span>
28 * Explanation:
29 * <ol>
30 * Operation 1: we choose i = 2 so c is 'b', then we remove s[1] as it is closest 'b' character to the left of s[1].<br />
31 * s becomes "cbd" after this.
32 * </ol>
33 * </div>
34 * <strong class="example">Example 3:
35 * <div class="example-block">
36 * Input: <span class="example-io">s = "baadccab"</span>
37 * Output: 4
38 * Explanation:
39 * <ol>
40 * Operation 1: we choose i = 6 so c is 'a', then we remove s[2] as it is closest 'a' character to the left of s[6].<br />
41 * s becomes "badccab" after this.
42 * Operation 2: we choose i = 0 so c is 'b', then we remove s[6] as it is closest 'b' character to the right of s[0].<br />
43 * s becomes "badcca" fter this.
44 * Operation 2: we choose i = 3 so c is 'c', then we remove s[4] as it is closest 'c' character to the right of s[3].<br />
45 * s becomes "badca" after this.
46 * Operation 1: we choose i = 4 so c is 'a', then we remove s[1] as it is closest 'a' character to the left of s[4].<br />
47 * s becomes "bdca" after this.
48 * </ol>
49 * </div>
50 *
51 * Constraints:
52 *
53 * 1 <= s.length <= 100
54 * s contains only lowercase English letters
55 *
56 */
57pub struct Solution {}
58
59// problem: https://leetcode.com/problems/minimize-string-length/
60// discuss: https://leetcode.com/problems/minimize-string-length/discuss/?currentPage=1&orderBy=most_votes&query=
61
62// submission codes start here
63
64impl Solution {
65 pub fn minimized_string_length(s: String) -> i32 {
66 0
67 }
68}
69
70// submission codes end
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn test_2716() {
78 }
79}
80
Back
© 2025 bowen.ge All Rights Reserved.