Skip to content

Commit 576573d

Browse files
committed
1750. Minimum Length of String After Deleting Similar Ends
1 parent 8bcf29e commit 576573d

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,3 +1318,4 @@ mod s1744_can_you_eat_your_favorite_candy_on_your_favorite_day;
13181318
mod s1745_palindrome_partitioning_iv;
13191319
mod s1748_sum_of_unique_elements;
13201320
mod s1749_maximum_absolute_sum_of_any_subarray;
1321+
mod s1750_minimum_length_of_string_after_deleting_similar_ends;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* [1750] Minimum Length of String After Deleting Similar Ends
3+
*
4+
* Given a string s consisting only of characters 'a', 'b', and 'c'. You are asked to apply the following algorithm on the string any number of times:
5+
* <ol>
6+
* Pick a non-empty prefix from the string s where all the characters in the prefix are equal.
7+
* Pick a non-empty suffix from the string s where all the characters in this suffix are equal.
8+
* The prefix and the suffix should not intersect at any index.
9+
* The characters from the prefix and suffix must be the same.
10+
* Delete both the prefix and the suffix.
11+
* </ol>
12+
* Return the minimum length of s after performing the above operation any number of times (possibly zero times).
13+
*
14+
* Example 1:
15+
*
16+
* Input: s = "ca"
17+
* Output: 2
18+
* Explanation: You can't remove any characters, so the string stays as is.
19+
*
20+
* Example 2:
21+
*
22+
* Input: s = "cabaabac"
23+
* Output: 0
24+
* Explanation: An optimal sequence of operations is:
25+
* - Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".
26+
* - Take prefix = "a" and suffix = "a" and remove them, s = "baab".
27+
* - Take prefix = "b" and suffix = "b" and remove them, s = "aa".
28+
* - Take prefix = "a" and suffix = "a" and remove them, s = "".
29+
* Example 3:
30+
*
31+
* Input: s = "aabccabba"
32+
* Output: 3
33+
* Explanation: An optimal sequence of operations is:
34+
* - Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".
35+
* - Take prefix = "b" and suffix = "bb" and remove them, s = "cca".
36+
*
37+
*
38+
* Constraints:
39+
*
40+
* 1 <= s.length <= 10^5
41+
* s only consists of characters 'a', 'b', and 'c'.
42+
*
43+
*/
44+
pub struct Solution {}
45+
46+
// problem: https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/
47+
// discuss: https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/discuss/?currentPage=1&orderBy=most_votes&query=
48+
49+
// submission codes start here
50+
51+
impl Solution {
52+
pub fn minimum_length(s: String) -> i32 {
53+
let mut q = s.chars().collect::<std::collections::VecDeque<char>>();
54+
55+
while q.len() > 1 && q.front() == q.back() {
56+
let cur = q[0];
57+
while q.front() == Some(&cur) {
58+
q.pop_front();
59+
}
60+
while q.back() == Some(&cur) {
61+
q.pop_back();
62+
}
63+
}
64+
65+
q.len() as i32
66+
}
67+
}
68+
69+
// submission codes end
70+
71+
#[cfg(test)]
72+
mod tests {
73+
use super::*;
74+
75+
#[test]
76+
fn test_1750_example_1() {
77+
let s = "ca".to_string();
78+
79+
let result = 2;
80+
81+
assert_eq!(Solution::minimum_length(s), result);
82+
}
83+
84+
#[test]
85+
fn test_1750_example_2() {
86+
let s = "cabaabac".to_string();
87+
88+
let result = 0;
89+
90+
assert_eq!(Solution::minimum_length(s), result);
91+
}
92+
93+
#[test]
94+
fn test_1750_example_3() {
95+
let s = "aabccabba".to_string();
96+
97+
let result = 3;
98+
99+
assert_eq!(Solution::minimum_length(s), result);
100+
}
101+
}

0 commit comments

Comments
 (0)